Regular Expressions in JavaScript
1. What is a regular expression?
A Regular Expression (Regular Expression) is an object that describes the character pattern. The RegExp class of ECMAcript represents a Regular Expression, both String and RegExp define functions that use strong regular expressions for pattern matching and text retrieval and replacement.
2. Create a regular expression
Method 1
Var box = new RegExp ('box'); // The first is the parameter string.
Var box = new RegExp ('box', 'ig ') // The second is the optional mode modifier.
Method 2
Var box =/box/; // directly use the backslash
Var box =/box/ig; // Add a pattern modifier after the second backslash
3. Test the regular expression.
The RegExp object contains two methods: test () and exec () for testing string matching.
Test ()
The test () method finds whether a specified regular expression exists in the string and returns a Boolean value. If yes, true is returned. Otherwise, false is returned.
Exec ()
The exec () method is also used to find a regular expression in a string. If the exec () method is successfully executed, information about the string to be searched is returned. If the exec () method fails, null is returned.
/Example of the test method using the new operator/
Var pattern = new RegExp ('box', 'I'); // create a regular expression, case insensitive
Var str = "This is a Box !"; // Create the string to be compared
Alert (pattern. test (str); // use the test () method to verify the matching result.
/* Example of the test method using the literal Method */var pattern =/box/I; // create a regular expression, not case sensitive var str = "This is a Box! "; Alert (pattern. test (str);/* use exec to return the matching array */var pattern =/box/I; var str =" This is a Box! "; Alert(pattern.exe c (str); // The returned array is matched; otherwise, null is returned.
Use the regular expression of a string
4. Access Control
The metacharacter of a regular expression is a character that contains special meanings. They have some special functions to control the matching mode. The metacharacter after the backslash will lose its special meaning.
Greed and laziness
The syntax difference between greedy mode and lazy mode is whether there is a question mark after the modifier is repeated. If there is a lazy mode, otherwise it is greedy mode.
Greedy quantifiers consume all the characters first, and then spit them out one by one until the match is successful.
A lazy quantizer is a single character from the beginning until the match is successful. That is, greed gradually matches forward, while laziness slowly matches backward.
Example:
Var pattern =/[a-z] +? ///? If greedy match is disabled, only the first matching is replaced.
Var str = 'aj1_djnfasdfdasdgh ';
Var result = str. replace (pattern, 'xxx ');
Alert (result); // returns xxxj1_djnfasdfdasdgh
Var pattern =/8 (. + ?) 8/g; // greedy is disabled.
Var str = 'this is 8abc8, that is 8abc8, there is 8abc8 ';
Var result = str. replace (pattern ,'$1');
Document. write (result); // return this is abc, that is abc, there is abc
Summary:
In my opinion, the regular expression can be used to verify on the client side, which can relieve the pressure on the server side before verification. The understanding of regular expressions is just a rough understanding. In the future, we need to constantly improve our practices.