JavaScript RegExp Object RegExp Object
The RegExp object represents a regular expression, which is a powerful tool for performing pattern matching on strings.
Direct volume syntax
/pattern/attributes
Syntax for creating REGEXP objects:
New RegExp (patternattributes);
Parameters
Parameter pattern is a string that specifies the pattern of a regular expression or other regular expression.
The parameter attributes is an optional string that contains the property "G", "I", and "M", respectively, for specifying global matching, case-sensitive matching, and multiline matching. The M attribute is not supported until ECMAScript is normalized. If pattern is a regular expression, not a string, you must omit the parameter.
return value
A new RegExp object that has the specified pattern and flags. If the parameter pattern is a regular expression instead of a string, the RegExp () constructor creates a new RegExp object with the same pattern and flags as the specified REGEXP.
Without the new operator, and REGEXP () as a function call, it behaves as if it were called with the new operator, except that when pattern is a regular expression, it only returns pattern, and no longer creates a new The RegExp object.
Thrown
SyntaxError-Throws the exception if pattern is not a valid regular expression, or if the attributes contains characters other than "G", "I", and "M".
TypeError-Throws the exception if pattern is a RegExp object but does not omit the attributes parameter.
Modifier
| modifier |
Description |
| I |
Performs a match that is not case sensitive. |
| G |
Performs a global match (finds all matches rather than stops after the first match is found). |
| M |
Performs multi-line matching. |
Square brackets
Square brackets are used to find characters in a range:
| An expression |
Description |
| [ABC] |
Look for any characters between square brackets. |
| [^ABC] |
Look for any characters that are not in square brackets. |
| [0-9] |
Look for any number from 0 to 9. |
| [A-z] |
Finds any characters from a to lowercase z from a small write. |
| [A-z] |
Look for any characters from uppercase A to uppercase Z. |
| [A-z] |
Look for any characters from uppercase A to lowercase z. |
| [ADGK] |
Finds any character within a given collection. |
| [^ADGK] |
Finds any character outside the given collection. |
| (Red|blue|green) |
Finds any of the specified options. |
Metacharacters
Metacharacters (metacharacter) is a character that has a special meaning:
| Meta character |
Description |
| . |
Finds a single character, in addition to line breaks and line terminators. |
| \w |
Find the word character. |
| \w |
Finds non-word characters. |
| \d |
Find numbers. |
| \d |
Finds non-numeric characters. |
| \s |
Find white space characters. |
| \s |
Finds non-whitespace characters. |
| \b |
Matches the word boundary. |
| \b |
Matches a non-word boundary. |
| / |
Find NUL characters. |
| \ n |
Find line breaks. |
| \f |
Find a page break. |
| \ r |
Look for the carriage return character. |
| \ t |
Find tabs. |
| \v |
Find vertical tabs. |
| \xxx |
Find the characters that are specified in octal number XXX. |
| \xdd |
Finds the characters specified in hexadecimal number DD. |
| \uxxxx |
Finds the Unicode characters specified in hexadecimal number xxxx. |
Quantifiers
| quantifier |
Description |
| n+ |
Matches any string that contains at least one n. |
| N |
Matches any string that contains 0 or more N. |
| N? |
Matches any string that contains 0 or one n. |
| N{X} |
Matches a string that contains a sequence of X N. |
| N{x,y} |
Matches a string containing a sequence of X to Y N. |
| N{x,} |
Matches a string that contains at least X N of a sequence. |
| n$ |
Matches any string that ends with N. |
| ^n |
Matches any string that begins with N. |
| !=0 |
Matches any string immediately followed by the specified string n. |
| ?! N |
Matches any string that does not follow the specified string n immediately thereafter. |
RegExp Object Properties
| Properties |
Description |
FF |
IE |
| Global |
RegExp whether the object has a flag g. |
1 |
4 |
| IgnoreCase |
RegExp whether the object has a flag I. |
1 |
4 |
| LastIndex |
An integer that indicates the character position at which to start the next match. |
1 |
4 |
| Multiline |
RegExp whether the object has a flag m. |
1 |
4 |
| Source |
The source text of the regular expression. |
1 |
4 |
RegExp Object Methods
| Method |
Description |
FF |
IE |
| Compile |
Compiles the regular expression. |
1 |
4 |
| Exec |
Retrieves the value specified in the string. Returns the found value and determines its location. |
1 |
4 |
| Test |
Retrieves the value specified in the string. Returns TRUE or FALSE. |
1 |
4 |
Methods for String objects that support regular expressions
| Method |
Description |
FF |
IE |
| Search |
Retrieves a value that matches a regular expression. |
1 |
4 |
| Match |
Finds a match for one or more regular expressions. |
1 |
4 |
| Replace |
Replace the substring that matches the regular expression. |
1 |
4 |
| Split |
Splits a string into an array of strings. |
1 |
4 |
Transferred from: http://www.w3school.com.cn/jsref/jsref_obj_regexp.asp
JavaScript REGEXP Objects