JS - Regular Expression

foreword

Regular expressions are often used in work, such as verifying email addresses, mobile phone numbers, verifying Chinese, removing leading and trailing spaces, and complex examples include the old jquery css selector engine sizzle.js.

I have always been half-aware of regular rules, and I only use the most basic ones, and I still use them ignorantly. It seems that people who are proficient in regularity are just as good as those who are proficient in the linux shell command line, haha.

Our use of regularity is often used to detect whether a string conforms to a certain format, or to match some attributes we need, such as className, transform value, and so on.

The following are some generalizations of the regular, I hope to be helpful to the judge.

1. Commonly used regular grammar

① Square brackets are used to find characters within a range.

Note: ^ in square brackets and ^ in quantifiers have different meanings. The ^ in square brackets is similar to the meaning of logical negation in programming languages, and the ^n in the quantifier means to match the string starting with n

[0-9]      // Find numbers from 0 to 9 
[Az]      // Find characters from big A to small z 
[abc]     // Find any character between square brackets. 
[^abc]   // Find any character not between square brackets.

②Meta characters, which represent characters with special meanings

A few commonly used:

\w — find word characters, mnemonic: short for word

\d — find a digit character, mnemonic: shorthand for digit

\s — find whitespace characters, mnemonic: short for space

\b — matches word boundaries, mnemonic: shorthand for border

These four are commonly used. When we play CF, we need WASD to move, except for A. When playing LOL, press B to return to the city. When appropriate, you can also use the game to associate, hehe.

When the above four metacharacters are in uppercase mode, their meanings are opposite to those in lowercase mode. For example: \W is to find non-word characters, \D is to find non-numeric characters.

③ Quantifier, used to match the corresponding quantity.

The use of quantifiers is to add symbols before and after characters to specify the number.

Notice:? =, ? When ! is used, it needs to be written in parentheses, for example: /1(?=23)/.test('123'), /1(?!3)/.test('123')

// for quantity matching

// The quantifier after the character 
+      // The match contains 1 or more, which can be understood as 1+ (0 - N) 
*       // The match contains 0 or more, which can be understood as 1 * (0 -N) 
?       // The match contains 0 or 1, which can be understood as whether it contains 

{X}            // The match contains X 
{X, Y}        // The match contains at least X and at most Y 
{X,}          // The match contains at least X

// Quantifier used to match start or end 
^n    // match starts with n 
n$    // match ends with n

// Used to match if a character is followed by a string, 
?= n matches any string followed by n
 ?!n matches any string not followed by n

Second, use.

① There are three methods of the RegExp object: complie, test, exec

The compile method, which is commonly used to change or recompile regular expressions during code execution, does not feel useful.

The test method is used to determine whether the string conforms to the rules of the regular expression, and returns true or false.

var reg = / man / g;
var str = 'woman' ;

reg.test(str);

The exec method is used to retrieve the matches of the regular expression in the string. If the match is successful, the return value is an array containing the matching results. Returns null if no corresponding match is found.

exec() takes one argument, the string to apply the pattern to, and returns an array containing information about the first match; or null if there is no match.

The returned array, although an instance of Array, contains two additional properties: index and input.

index represents the position of the match in the string, and input represents the string to which the regular expression is applied.

var text = "cat, bat, sat, fat";
var pattern2 = /.at/g;

var matches = pattern2.exec(text);
alert(matches.index); //0
alert(matches[0]); //cat
alert(pattern2.lastIndex); //3

matches = pattern2.exec(text);
alert(matches.index); //5
alert(matches[0]); //bat
alert(pattern2.lastIndex); //8

 

② Methods of String objects that support regular expressions: search, split, replace, match

The search method is used to retrieve the specified substring in the string. Returns the starting position of the first substring that matches regexp if the match is successful, otherwise returns -1. Similar to String.prototype.indexOf method. I feel indexOf is better.

split method, which splits a string into an array. No description is given.

The replace method replaces the content in the string. There are two parameters, the first can be a regular expression, or a string, and the second parameter can be the replacement text, or a function that generates the replacement text.

When the second parameter is a function, the function also has parameters. The first parameter of the function, which defaults to the match of the pattern, that is, the value matched by the regular expression.

Note: In the second parameter of the replace method, the $ character has a specific meaning. For details, see W3C

// swap two words in string 
var str = 'hello,world' ;
str.replace(/(\w+)\S(\w+)/, '$2 $1') //world hello;

// Use functions to modify the matched characters, such as borderTop in DOM is modified to border-top in css 
function upperToHypenLower(match){
     return '-' + match.toLowerCase();
}
function styleHypenFormat(propertyName){
  return propertyName.replace(/[A-Z]/g, upperToHypenLower);
}
var style = styleHypenFormat('borderTop'); // 'border-top'

match method, 

The match() method returns an array with several static properties on the array, index, input, groups. The return value of str.match(reg) and reg.exec(str) is the same.

 

Paste some commonly used regular expressions

Match Chinese: [\u4e00-\u9fa5] // The range of Chinese ACALL codes The 
first and last spaces of the line: ^\s*|\s*$ // Remove the first and last spaces, trim() polyfill. 

Mobile phone number: /^1\d{10}$/ 
Email: ^\w+@[a-z0-9]+(\.[az]+){1,3 }$  
 // Start with at least one character ( \w letter, number or underscore), then match @, followed by any letter or number, \. represents a real dot, followed by at least one character (az), and this (such as .com) is a sub Item as an end, can appear 1-3 times. 
Because some mailboxes are like this.cn.net. ([email protected] [email protected] [email protected] )
URL: [a -zA-z]+: // [^\s]* http://..... . // Matches any letter that is not case sensitive, followed by //, followed by any character that is not a space Postal code: [ 1-9]\d{5} // The starting number cannot be 0, then 5 numbers ID card: [1-9]\d{14}|[1-9]\d{17}|[1-9]\d{16}x

 

Summarize

str.match(reg) has the same effect as reg.exec(str).

 

refer to

http://www.w3school.com.cn/jsref/jsref_obj_regexp.asp

https://www.cnblogs.com/moqing/archive/2016/07/13/5665126.html

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325331636&siteId=291194637