Brother, I have been learning JavaScript recently. When I learned regular expression, I found that the knowledge points were a bit confusing, so I wrote a blog to summarize it.
Definition
There are two ways to define reg exp in JavaScript:
1) Use new exp: var exp1 = new exp("abc");
2) Place pattern directly between the two /s: var exp2 = /abc/; //Note. . There are no double quotation marks. If you add them, it becomes a string.
Special characters
Visual inspection shows that the special characters are the same as those in Perl. . Just 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 except newlines
There is a very simple way to remember:
d = digit So it’s numbers
w = word So it’s letters
s = space So it’s spaces
All uppercase letters are reversed. .
Brackets []
Putting pattern in the brackets means that it is true as long as it matches any character. (Same as java or Perl)
For example,
console.log(/[01]/.test("023424")); // true
console.log(/[01]/.test("13424")) ; // true
console.log(/[01]/.test("23424")); // false
brackets ()
means It must match everything in the brackets to be true
For example,
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
are the same as java. . This watch is very good. . Brother, I always like to use
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, exactly n times |
X{n,} |
X{n,}? |
X{n,} |
X, at least n times |
X{n,m} |
X{n,m}? |
X{n,m} |
X, at least n but not more thanm times |
expression object functions
1) test This is very simple, just put the string to be tested in test(...), this function will return true/false Represents match/unmatch
2) exec, this function returns null if the matching string is not found.. If found, it will return an array. This contains the matching strings in order
3 ) String.replace(expression1, string1) This function replaces the match part in expression with string1. In string1, the parenthesized group
in the previous expression can be used to replace a certain part of it. For example, "co-ol".replace(/[w] -[w] /,"$2-$1"); //"ol-co" can be used until $9
4)String.replace (expression, function) This is an enhanced version, and it is very powerful. You can define any output you want through function. The specific usage is not listed here, please refer to
Click to open the link
Dynamic generation of reg expression
When the things you want to use in reg exp are only known by runtime , this method can be applied
to generate reg exp. In fact, you only need to use string to build the appearance of reg exp, and then use the constructor of Exp. (Mentioned at the beginning of the article)
For example:
var name = "dear"
"oh, my dear".replace(new Exp(name), "god"); // oh, my god
But if there are special characters in the name that may be used in regular expressions, the above method will often go wrong.
So, in that case, we can add a backslash in front of each character of the input string, for example:
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