Younger brother I recently in learning JavaScript, learn to regular expression when the knowledge point is a bit messy, so special write a blog to make a summary.
Defined
There are two ways to define REG exp in javascript:
1) with new Exp:var EXP1 = new exp ("ABC");
2) directly in two/intermediate pattern:var EXP2 =/abc/; Attention.. No double quotes, no strings added.
Special characters
Visual special characters are the same as Perl's. Just use it for me.
\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 is the letter
s = Space so it is a blank
All caps are all reversed.
Brackets []
The pattern representation in parentheses is true as long as any character is met. (Same as Java or Perl)
Like what
Copy Code code as follows:
Console.log (/[01]/.test ("023424")); True
Console.log (/[01]/.test ("13424")); True
Console.log (/[01]/.test ("23424")); False
Brackets ()
It means to conform to all the parentheses.
Like what
Copy Code code 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
And Java is the same as the same. This watch is very good. Brother, I always like to use
Greedy |
Reluctant |
possessive |
meaning |
X? |
X?? |
x?+ |
X, once or not in 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 simple, just put the string to test to test (...) , this function will return True/false representative Match/unmatch.
2 Exec, this function returns null if match string is not found. If found, it will return an array. This contains a match in sequence string
3) String.Replace (expression1, string1) This function replaces the match part of expression with string1, String1 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 always use the $
4 string.replace (expression, function) This is a strong version and is powerful and can be defined by function to any output you want. The specific usage is not listed here, please refer to
Click to open the link
Dynamically generating Reg expression
This method works when you want to use something in Reg exp that only runtime knows.
To generate REG Exp, you only need to use a string to build the appearance of Reg Exp, and then use exp constructor. (mentioned at the beginning of the article)
For example:
Copy Code code 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 that may be used in regular expression, the top method is often wrong.
So, in that case, we can add a backslash to the front of each character of the input string, such as:
Copy Code code 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