Objective
Regular expression this thing, in fact, work often used, simple such as verification mailbox, mobile phone number, verification of Chinese, remove the first and the white space, complex examples of the old jquery CSS selector engine sizzle.js.
The regular is always smattering, will only use the most basic, but also uses the Mengmengdongdong. The person who feels proficient is the same as the man who is proficient in the Linux shell command line, haha.
Our use of the regular is often used to detect whether a string conforms to a certain format, or to match some of the attributes we need, such as className, transform values, and so on.
Here are some generalizations about the regular, hoping to help crossing.
First, the regular common grammar
① square brackets, which are used to find characters within a range.
Note: The ^ and quantifier meanings in square brackets are different. ^ in square brackets is similar to the logical non-meaning in a programming language, and the ^n in a quantifier represents a string that matches the beginning of n
[0-9] // find numbers from 0 to 9 [A-z] // find characters from large A to small z [ABC] // finds any character between square brackets. [^ABC] // find any characters that are not in square brackets.
② metacharacters, which represent characters with special meanings
A few of the usual:
\w-find word characters, mnemonic method: Word Shorthand
\d-find numeric characters, mnemonic method: shorthand for digit
\s-Find white space characters, mnemonic method: shorthand for space
\b-matching word boundaries, mnemonic method: shorthand for border
Commonly used on these 4 bar, we play CF when the move is to WASD, in addition to a others are used. When playing lol, press B to return to the town. When appropriate, you can also use the game to associate, hey.
When the above 4 metacharacters are in uppercase mode, the meaning is the opposite of lowercase mode. For example: \w is looking for non-word characters, \d is looking for non-numeric characters.
③ quantifier, used to match the corresponding quantity.
The use of quantifiers is to add symbols before and after the characters, specifying the number.
Attention:? =,?! When used, it needs to be written in parentheses, for example:/1 (? =23)/.test (' 123 '),/1 (?!). 3)/.test (' 123 ')
//for quantity matching//the quantifier following the character+//match contains 1 or more, which can be understood as 1+ (0-n)*//Match contains 0 or more, can be understood as 1 * (0-N)?//The match contains 0 or 1, which can be understood as whether it contains{X}//Match contains X{X, Y}//match contains at least X, up to Y{X,}//match contains at least X//a quantifier used to match the beginning or end^n//match begins with nn$//match End is n//whether to follow a string after a character is matched,?=n matches any string followed by n?! n matches any string that is not followed by n
Second, use.
①regexp object methods, there are three: Complie, test,exec
The compile method, commonly used with code execution to change or recompile regular expressions, does not feel useful.
The test method, which is used to determine whether a string conforms to the regular expression's rules and returns TRUE or false.
var reg =/man/g; var str = ' woman '; reg.test (str);
The Exec method, which is used to retrieve the matching of regular expressions in a string. If the match succeeds, the return value is an array that holds the matching result. If no corresponding match is found, NULL is returned.
EXEC () takes a parameter, which is the string to which the pattern is applied, and then returns an array that contains the first occurrence of the information, or returns NULL if there is no match.
The returned array, although an instance of array, contains two additional attributes: Index and input.
Index represents the position of the match in the string, and input represents the string that applies the regular expression.
If it is a global match, you can find each match to the string, and the subkey, through the while loop. Each match is followed by the last position to begin matching
varText = "Cat, bat, Sat, fat";varPATTERN2 =/.at/G;varMatches =pattern2.exec (text); alert (matches.index);//0Alert (matches[0]);//Catalert (Pattern2.lastindex);//3matches=pattern2.exec (text); alert (matches.index);//5Alert (matches[0]);//Batalert (Pattern2.lastindex);//8
② Methods for String objects that support regular expressions: Search,split, replace, match
The search method that is used to retrieve the substring specified in the string. If the match succeeds, returns the starting position of the first substring that matches the regexp, otherwise returns-1. Similar to the String.prototype.indexOf method. Feel indexOf better.
Split method to split the string into arrays. Not described.
Replace method, replacing the contents of the string. There are two parameters, the first of which can be a regular expression, or a string, the second argument can be a replacement text, or a function that generates alternate text.
When the second argument is a function, the function also has parameters. The first parameter of the function, which defaults to the pattern's match, which is the value that the regular expression matches to.
Note in the second parameter of the Replace method, the $ character has a specific meaning. Specifically, you can view the
//swap two words in a stringvarstr = ' Hello,world '; Str.replace (/(\w+) \s (\w+)/, ' $ $ $ ')//World Hello;//use functions to modify the characters that match to, such as bordertop in the DOM modified to border-top in CSSfunctionUppertohypenlower (match) {return'-' +match.tolowercase ();}functionStylehypenformat (PropertyName) {returnPropertyname.replace (/[a-z]/g, Uppertohypenlower);}varstyle = Stylehypenformat (' bordertop ');//' Border-top '
The match method, the match () method returns an array or null.
If the regular expression is not a global match, there are several static properties on the returned array, index,input,groups, at which time the return value of Str.match (REG) and reg.exec (str) is the same.
Paste some common regular expressions
Match Chinese: [\u4e00-\u9fa5]//the range of Chinese Acall codeLine trailing space: ^\s*|\s*$//Remove the Polyfill of the leading and trailing blanks, trim (). phone Number:/^1\d{10}$/Email:^\[email protected][a-z0-9]+ (\.[ a-z]+) {1,3}$ //start at least one character (\w letters, numbers, or underscores), and then match @, followed by any letter or number, \. Represents the real point,. The character behind is at least one (A-Z), and this (for example,. com) whole is the end of a subkey, which can occur 1-3 times.
Because some of the mailboxes are like this. cn.net. ([email protected] [email protected] [email protected])URL: [A-za-z]+://[^\s]*/http/... //match any letter that is not case, followed by//, any character that is not a space followed byzip Code: [1-9]\D{5}//The starting number cannot be 0, then 5 digitsID: [1-9]\d{14}| [1-9]\d{17}| [1-9]\d{16}x
Reference
Http://www.w3school.com.cn/jsref/jsref_obj_regexp.asp
Https://www.cnblogs.com/moqing/archive/2016/07/13/5665126.html
js-Regular Expressions