I recently learned javaScript and learned the regular expression. The knowledge is messy, so I wrote a blog to make a summary.
Definition
There are two methods to define reg exp in javascript:
1) Use new exp: var exp1 = new exp ("abc ");
2) Put pattern: var exp2 =/abc/; // In the middle of the two directories .. Without Double quotation marks, the string is added.
Special characters
Special characters are the same as those in perl .. Use it directly.
\ D Digit characters
\ W Alphanumeric characters ("word characters ")
\ S Whitespace characters (space, tab, newline, and similar)
\ D Characters that are not digits
\ W Non-alphanumeric characters
\ S Non-whitespace characters
. A period matches all characters within T newlines
There is a simple way to remember:
D = digit, so it's a number.
W = word, so it is a letter
S = space, so it is a space.
All uppercase letters are reversed ..
Brackets []
Put pattern in brackets to indicate that all characters that match any character are true. (Same as java or Perl)
For example
Copy codeThe Code is as follows:
Console. log (/[01]/. test ("023424"); // true
Console. log (/[01]/. test ("13424"); // true
Console. log (/[01]/. test ("23424"); // false
Parentheses ()
It indicates that all the values in the brackets are true.
For example
Copy codeThe Code is as follows:
Console. log (/[01]/. test ("013424"); // true
Console. log (/[01]/. test ("13424"); // false
Console. log (/[01]/. test ("230424"); // false
Console. log (/[01]/. test ("230142401"); // true
Quantifiers
It is the same as java .. This table is very good .. I have always liked it.
Greedy |
Reluctant |
Possessive |
Meaning |
X? |
X ?? |
X? + |
X, once or not at all |
X * |
X *? |
X * + |
X, zero or more times |
X + |
X ++? |
X ++ |
X, one or more times |
X {n} |
X {n }? |
X {n} + |
X, exactlyNTimes |
X {n ,} |
X {n ,}? |
X {n,} + |
X, at leastNTimes |
X {n, m} |
X {n, m }? |
X {n, m} + |
X, at leastNBut not moreMTimes |
Expression object functions
1) test: Put the string to test in test (...). This function returns true/false to indicate match/unmatch.
2) exec, the function returns null. If the match string is not found, an array will be returned if the match string is found. This contains the string matching in order.
3) String. replace (expression1, string1) This function replaces the match part in expression with string1, And the parenthesized group in the previous expression can be used in string1.
To replace a part. For example, "co-ol ". replace (/[\ w] + \-[\ w] +/, "$2-$1"); // "ol-co" always available $9
4) String. replace (expression, function) is an enhanced version. It is very powerful and can be used to define any output you want. The specific usage is not listed here. Please refer
Click Open Link
Dynamically generate reg expression
This method can be used when you want to know only runtime in reg exp.
To generate reg exp, you only need to build reg exp with string, and then use Exp's constructor. (Mentioned at the beginning of the article)
For example:
Copy codeThe Code is as follows:
Var name = "dear"
"Oh, my dear". replace (new Exp (name), "god"); // oh, my god
However, if the name contains special characters that may be used in the regular expression, the above method will often fail.
Therefore, in that case, we can add a backslash to the front of each character in the input string, for example:
Copy codeThe Code is as follows:
Var name = df [] vxv;
Var expName = name. replace ("/[^/w/s]/g &");
"My name is df [] vxv". replace (new Exp (name), "Bob"); // my name is Bob