Get the App
SLTechnology News&Howtos  ›  Development  › 

What are the relevant knowledge points of regular expressions in JavaScript

Shulou Source: shulou.com Published: 2022-06-01 03:32:31 10月04日 Update

Xiaobian to share with you what are the relevant knowledge points of regular expressions in JavaScript, I believe most people still do not know how, so share this article for everyone's reference, I hope you have a lot of gains after reading this article, let's go to understand it together!

JavaScript regular expressions 1. Creating regular expressions

Literally. syntax: Reg = /pattern/modifiers; literal regularization consists of two positive oblique bars//, the first positive oblique bar followed by rules: /pattern[rules can write a variety of metacharacters| quantifier| character set| Assertion…] Write identifier/modifiers[g global match] after the second positive slant| i Ignore case| m-line matching| ^Starting position| $End Position] identifier.

var Reg = /box/gi;

Constructor. Syntax Reg = new RegExp( pattern , modifiers ); pattern, modifiers is a string in this case. What methods are created is the same, pattern templates, what to match, modifiers.

var Reg = new RegExp("box","gi");2.String Regular expression method description String.match(Reg) Returns an array containing all strings matched by RegExp or nullString.search(Reg) Returns the location of the first occurrence of a RegExp matching string String.replace(Reg, newStr) replaces the RegExp matching result with newStr and returns a new string String.split(Reg) Returns an array of strings split by the specified RegExp var str = 'a1b2c3a4a5', reg = /a/g;console.log(str.match(reg)); //["a", "a", "a"]var str = 'a1b2c3a4a5', reg = /a/;console.log(str.search(reg)); //0var str = 'a1b2c3a4a5', reg = /a/g;console.log(str.replace(reg,function(){ console.log(arguments); return 5555;}));var str = 'a,b,c,d', reg = /,/g; //console.log (str.split (',')); //["a", "b", "c", "d"]console.log(str.split(reg)) //["a", "b", "c", "d"]3.RegExp object's method method description RegExp.exec(String) Performs a match search in a string and returns an array of first matches RegExp.test(String) tests a pattern match in a string and returns true or false4. modifier

Modifiers, also called identifiers, specify matching patterns, modifiers are used to perform case sensitivity and global matching.

i Case matching ignored.

g matches globally, no g matches only the first element, no matching.

m Perform multi-row matching

var patt = /pattern/i; //ignore case matching var patt = /pattern/g; //global match var patt = /pattern/m; //perform multiline matching 5. metacharacters

A special character that has a special meaning in a regular expression.

Special translation characters. \ /。

. A single arbitrary character, except for newline\n and tab\r \escape characters, to escape symbols with special meaning to ordinary symbols: \.\ d Numeric [0~9]\D Non-numeric\s Space\S Non-space\w Character [Letter| digital| underscore]\W Non-character\b word boundary (except (word) alphanumeric_counts as word boundary) \B Non-word boundary var reg = /\./;// Match.var reg = /\\/;//Match\var reg = /\/;//Match/var str = '\\';var reg = /\\/g;console.log(reg.test(str)); //true The above is "What are the relevant knowledge points of regular expressions in JavaScript" All the contents of this article, thank you for reading! I believe that everyone has a certain understanding, hope to share the content to help everyone, if you still want to learn more knowledge, welcome to pay attention to the industry information channel!

Tags: Characters regular strings expressions methods global size numbers knowledge special location content words arrays slants identifiers identifiers patterns articles boundaries Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Xiaomi macOS NVidia Shulou Technology Microsoft