The RegExp object represents a regular expression, which is a powerful tool for matching string execution modes.
1. Create a RegExp object
Direct Syntax:
/Pattern/attributes
Syntax for creating a RegExp object:
New RegExp (pattern, attributes );
Note:
The pattern parameter is a string that specifies the regular expression mode or other regular expressions.
The attributes parameter is an optional string that contains the attributes "g", "I", and "m". It is used to specify global matching, case-sensitive matching, and multi-row matching, respectively. Before ECMAScript standardization, the m attribute is not supported. This parameter must be omitted if pattern is a regular expression rather than a string.
Return Value:
A new RegExp object with the specified pattern and flag. If the pattern parameter 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. It only returns pattern when pattern is a regular expression, instead of creating a new RegExp object.
Throw:
SyntaxError-If pattern is not a valid regular expression, or attributes contains characters other than "g", "I", and "m", this exception is thrown.
TypeError-If pattern is a RegExp object but the attributes parameter is not omitted, this exception is thrown.
2. Modifier
I. Perform case-insensitive matching.
G ).
M.
Take I as an example:
Var reg = new RegExp ("regexp", "I ");
Or:
Var reg =/regexp/I;
3. square brackets []
Square brackets are used to find characters in a certain range:
[Abc] searches for any character between square brackets. // Var patt1 =/[a-h]/g; match all characters from a to h
[^ Abc] searches for any characters that are not in square brackets.
[0-9] search for any number from 0 to 9.
[A-z] searches for any characters from lowercase to lowercase.
[A-Z] looks for any character from uppercase A to uppercase Z.
[A-Z] searches for any character from a to uppercase Z.
[Adgk] searches for any character in a given set.
[^ Adgk] searches for any character outside the given set.
[Red | blue | green] searches for any specified options.
4. metacharacters
Metacharacter is a character with special meanings:
. Find a single character, except for line breaks and line terminator.
\ W: Search for word characters.
\ W searches for non-word characters.
\ D.
\ D: searches for non-numeric characters.
\ S.
\ S.
\ B searches for matching at the beginning or end of a word.
\ B searches for matches that are not at the beginning or end of a word.
\ 0: Search for NUL characters.
\ N.
\ F.
\ R.
\ T.
\ V.
\ Xxx: Search for characters specified by Octal numbers xxx.
\ Xdd: Search for characters specified by dd in hexadecimal format.
\ Uxxxx searches for Unicode characters specified by the hexadecimal number xxxx.
5. quantifiers
N + matches any string containing at least one n.
N * matches 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 sequences.
N {X,} matches strings that contain at least X n sequences.
N $ matches any string ending with n.
^ N matches any string starting with n.
? = N matches any string followed by the specified string n.
?! N matches any string that is not followed by the specified string n.
6. RegExp Object Attributes
Whether the global RegExp object has a flag.
Whether the ignoreCase RegExp object has flag I.
LastIndex is an integer that indicates the next matching character position.
Whether the multiline RegExp object has a flag m.
Source Text of the source regular expression.
7. The compile () method is used to compile regular expressions during script execution.
RegExpObject. compile (regexp, modifier)
Regexp regular expression.
The type specified by modifier. "G" is used for global matching, "I" is used for case-sensitive, and "gi" is used for global case-sensitive matching.
Example:
Var str = "Every man in the world! Every woman on earth! ";
Patt =/man/g;
Str2 = str. replace (patt, "person ");
Document. write (str2 + "<br/> ");
Patt =/(wo )? Man/g;
Patt. compile (patt );
Str2 = str. replace (patt, "person ");
Document. write (str2 );
Output:
Every person in the world! Every woperson on earth!
Every person in the world! Every person on earth!
8. The test () method is used to check whether a string matches a certain pattern.
RegExpObject. test (string)
String is required. The string to be checked.
Return Value
If the string contains the text that matches RegExpObject, true is returned; otherwise, false is returned.
Example:
Var str = "Visit W3School ";
Var patt1 = new RegExp ("W3School ");
Var result = patt1.test (str );
Document. write ("Result:" + result );
Output:
Result: true
--------- Method of String object supporting regular expressions
Search retrieves the value that matches the regular expression.
Match finds matching of one or more regular expressions.
Replace replaces the substring that matches the regular expression.
Split splits the string into a string array.