Regexp object
The Regexp object represents a regular expression, which is a powerful tool for matching string execution modes.
Direct amount syntax
/Pattern/attributes
Syntax for creating a Regexp object:
New Regexp (Pattern,Attributes);
Parameters
ParametersPatternIs a string that specifies the regular expression mode or other regular expressions.
ParametersAttributesIt is an optional string that contains the "g", "I", and "M" attributes. It is used to specify global matching, case-sensitive matching, and multi-line matching, respectively. Before ecmascript standardization, the M attribute is not supported. IfPatternThis parameter must be omitted if it is a regular expression rather than a string.
Return Value
A new Regexp object with the specified pattern and flag. If the ParameterPatternIf it is a regular expression rather than a string, the Regexp () constructor creates a new Regexp object with the same pattern and flag as the specified Regexp.
If Regexp () is called as a function instead of the new operator, it performs the same behavior as when it is called using the new operator.PatternWhen it is a regular expression, it only returnsPatternInstead of creating a new Regexp object.
Throw
Syntaxerror-ifPatternIt is not a legal regular expression, orAttributesThis exception is thrown if it contains characters other than "g", "I", and "M.
Typeerror-ifPatternIt is a Regexp object, but it is not omittedAttributesParameter.
Modifier
Modifier |
Description |
I |
Perform case-insensitive matching. |
G |
Perform global match (search for all matches instead of stopping the first match ). |
M |
Perform multi-row matching. |
Square brackets
Square brackets are used to find characters in a certain range:
Expression |
Description |
[ABC] |
Find any character between square brackets. |
[^ ABC] |
Search for any characters that are not in square brackets. |
[0-9] |
Search for any number ranging from 0 to 9. |
[A-Z] |
Search for any characters from lowercase to lowercase. |
A-Z |
Search for any characters from uppercase A to uppercase Z. |
[A-Z] |
Search for any characters from uppercase A to lowercase Z. |
[Adgk] |
Searches for any character in a given set. |
[^ Adgk] |
Searches for any characters outside the given set. |
(Red | Blue | green) |
Find any specified options. |
Metacharacters
Metacharacter is a character with special meanings:
Metacharacters |
Description |
. |
Searches for a single character, except for line breaks and line Terminators. |
\ W |
Search for word characters. |
\ W |
Searches for non-word characters. |
\ D |
Search for numbers. |
\ D |
Searches for non-numeric characters. |
\ S |
Search for blank characters. |
\ S |
Searches for non-blank characters. |
\ B |
Match the word boundary. |
\ B |
Match non-word boundary. |
\ 0 |
Search for NUL characters. |
\ N |
Find a line break. |
\ F |
Find a break. |
\ R |
Find the carriage return. |
\ T |
Search for tabs. |
\ V |
Search for vertical tabs. |
\ Xxx |
Search for characters specified by octal XXX. |
\ Xdd |
Search for the characters specified by DD in hexadecimal notation. |
\ Uxxxx |
Find the Unicode characters specified by the hexadecimal number XXXX. |
Quantifiers
Quantifiers |
Description |
N + |
Match any string containing at least one N. |
N * |
Match any string containing zero or more N. |
N? |
Match any string containing zero or one N. |
N {x} |
Matches the string that contains x N sequences. |
N {x, y} |
Matches the string that contains X or Y n. |
N {X ,} |
Matches strings that contain at least x N sequences. |
N $ |
Match any string ending with N. |
^ N |
Match any string starting with N. |
? = N |
Match any string followed by the specified string n. |
?! N |
Match any string that is not followed by the specified string n. |
Regexp Object Attributes
FF: Firefox, ie: Internet Explorer
Attribute |
Description |
FF |
IE |
Global |
Whether the Regexp object has a flag. |
1 |
4 |
Ignorecase |
Whether the Regexp object has a flag I. |
1 |
4 |
Lastindex |
An integer that indicates the position of the next matched character. |
1 |
4 |
Multiline |
Whether the Regexp object has a flag M. |
1 |
4 |
Source |
The Source Text of the regular expression. |
1 |
4 |
Regexp object Method
FF: Firefox, ie: Internet Explorer
Method |
Description |
FF |
IE |
Compile |
Compile a regular expression. |
1 |
4 |
Exec |
Returns the value specified in the string. Return the value found and locate it. |
1 |
4 |
Test |
Returns the value specified in the string. Returns true or false. |
1 |
4 |
Methods for string objects that support regular expressions
FF: Firefox, ie: Internet Explorer
Method |
Description |
FF |
IE |
Search |
Returns the value that matches the regular expression. |
1 |
4 |
Match |
Find the matching of one or more regular expressions. |
1 |
4 |
Replace |
Replace the substring that matches the regular expression. |
1 |
4 |
Split |
Splits a string into a string array. |
1 |
4 |