Brother, I recently learned JavaScript and saw that the knowledge points were a little messy when I was learning regular expression, 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) Put pattern directly between two //: var exp2 = /abc/; //Note. . There are no double quotes, if you add it, it becomes a string
Special characters
Visually, special characters are the same as those of 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 a number
w = word so it's letters
s = space so it is a space
All capitalizations are inverse. .
brackets[]
Put pattern in brackets means that as long as any character is matched, it is true. (It's the same as java or Perl)
for example
The code copy is as follows:
console.log(/[01]/.test("023424")); // true
console.log(/[01]/.test("13424")); // true
console.log(/[01]/.test("23424")); // false
brackets()
It means that everything in brackets must be true
for example
The code copy 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's the same as java. . This table is very good. . I've always liked using 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, 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 than m times |
expression object functions
1) test is very simple. Just put the string to test in test(...), and this function will return true/false, which represents match/unmatch
2) exec, this function returns null if no match string is found. If it is found, an array will be returned. This contains strings of matches in order.
3) String.replace(expression1, string1) This function replaces the match part in the expression with string1. In string1, you can use the parenthesized group in the previous expression
, to replace a 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 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
Dynamically generate reg expression
This method can be applied when you want to use something in reg exp only knows about runtime
To generate reg exp, you only need to use string to create the reg exp look, and then use Exp's constructor. (mentioned at the beginning of the article)
For example:
The code copy is as follows:
var name = "dear"
"oh, my dear".replace(new Exp(name), "god"); // oh, my god
However, if there are special characters in the name, it may be used in regular expression, and the above method will often make an error.
So, in that case, we can prefix each character of the input string with a backslash such as:
The code copy 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