ArticleDirectory
- Syntax for creating a Regexp object:
- Direct amount syntax
- Syntax for creating a Regexp object:
- Parameters
- Return Value
- Throw
- Return Value
- Description
- Return Value
- Description
- Return Value
- Description
- Syntax
- Return Value
Create Regexp object Syntax: Direct amount syntax
/Pattern/attributes
Syntax for creating a Regexp object: var OBJ = new Regexp (pattern, attributes); Example 1:
VaR pattern =/A/; document.write(pattern.exe C ("asdas "));
Example 2:
VaR OBJ = new Regexp ("A"); document.write(obj.exe C ("asdas "));
The execution results of Case 1 and Case 2 are the same;
Parameters
ParametersPatternIs a string that specifies the regular expression mode or other regular expressions.
ParametersAttributesIs an optional string containing the attribute "G" (perform global match to find all matches rather than stop after the first match is found .) , "I": perform case-insensitive matching. And "M": perform multi-row matching ., It is used to specify global match, case-sensitive match, and multi-row match. 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.
Regexp object method: 1. Test (): retrieves the value specified in the string. Returns true or false.
VaR patt1 = new Regexp ("e"); document. Write (patt1.test ("the best things in life are free "));
2. Exec (): retrieves the value specified in the string. Return the value found and locate it.
Method to retrieve the specified value in the string. The returned value is the value found. If no matching is found, null is returned.
3. Compile (): You can change the search mode or add or delete the second parameter.
VaR patt1 = new Regexp ("e"); document. write (patt1.test ("the best things in life are free"); patt1.compile ("D"); document. write (patt1.test ("the best things in life are free "));
Modifier:
1. G: perform a global match to find all the matches instead of stopping the first match;
When the "G" parameter is used, exec () works as follows:
- Locate the first "E" and store its location
- If exec () is run again, search from the storage location, find the next "E", and store its location
VaR patt1 = new Regexp ("e", "G" cannot parse dow.result#patt1.exe C ("the best things in life are free"); document. Write (result);} while (result! = NULL)
Regexp Object Attributes
1: Global: whether the Regexp object has a flag.
2: whether the ignorecase: Regexp object has the flag I.
3: lastindex: an integer that indicates the start position of the next matching character.
4: multiline: indicates whether the Regexp object has a flag M;
5: Source: The Source Text of the regular expression.
VaR OBJ = new Regexp (/S/); document. Write (obj. source );
Methods for string objects that support regular expressions
1: The Search () method is used to retrieve the specified substring in the string or the substring that matches the regular expression.
Case Insensitive:
<SCRIPT type = "text/JavaScript"> var STR = "Visit w3school! "Document. Write (Str. Search (/w3school/I) </SCRIPT>
Return Value
The starting position of the first substring in stringobject that matches Regexp.
Note:If no matched substring is found,-1 is returned.
Description
The search () method does not perform global match. It ignores the flag. It also ignores the lastindex attribute of Regexp and always searches from the start of the string, which means it always returns the first matching position of stringobject.
2: The match () method can be used to retrieve the specified value in a string, or to find matching of one or more regular expressions. This method is similar to indexof () and lastindexof (), but it returns the specified value rather than the position of the string.
Return Value
An array that stores matching results. The content of this array depends on whether Regexp has a global flag.
Description
The match () method retrieves the stringobject string to find one or more texts that match Regexp. The behavior of this method depends largely on whether Regexp has a flag.
If Regexp does not mark g, the match () method can only perform one match in stringobject. If no matching text is found, match () returns NULL. Otherwise, it returns an array containing information related to the matched text it finds. The 0th elements in the array store the matching text, while the remaining elements store the text that matches the regular expression's subexpression. In addition to these regular array elements, the returned array also contains two object attributes. The index attribute declares the position of the starting character of the matching text in the stringobject, And the INPUT attribute declares the reference to the stringobject.
If Regexp has a flag, the match () method performs a global search and finds all matching substrings in stringobject. If no matched substring is found, null is returned. If one or more matched substrings are found, an array is returned. However, the content of the array returned by global match is very different from that returned by the former. Its array elements store all matched substrings in stringobject, and there is no index or INPUT attribute.
Note:In global search mode, match () does not provide text information that matches the subexpression, nor declare the position of each matched substring. You can use regexp.exe C () to retrieve global information ().
Example where the parameter is a string:
VaR STR = "Hello world! "Document. Write (Str. Match (" world ") +" <br/> ")
Example where the parameter is a regular expression:
VaR STR = "1 plus 2 equal 3" document. Write (Str. Match (/\ D +/g ))
3: The Replace () method is used to replace other characters with some characters in the string, or replace a substring that matches the regular expression.
Stringobject. Replace (Regexp/substr,Replacement)
Parameters |
Description |
Regexp/substr |
Required. Specifies the substring or Regexp object of the pattern to be replaced. Note that if the value is a string, it is used as the direct text mode to be retrieved, rather than being converted to a Regexp object first. |
Replacement |
Required. A string value. Specifies the function for replacing text or generating replacement text. |
Return Value
A new string is usedReplacementIt is obtained after the first match of Regexp or all matches.
Description
The Replace () method of the string stringobject performs the search and replace operation. It will search for the Child string that matches Regexp in stringobject, and then useReplacementTo replace these substrings. If Regexp has a global flag, the Replace () method replaces all matched substrings. Otherwise, it only replaces the first matched substring.
ReplacementIt can be a string or a function. If it is a string, each match will be replaced by a string. However, the $ character in replacement has a specific meaning. As shown in the following table, it indicates that the string obtained from pattern matching will be used for replacement.
Character |
Replace text |
$1, $2,..., $99 |
Text that matches the 1st to 99th subexpressions in Regexp. |
$ & |
The substring that matches the Regexp. |
$' |
Text on the left of the matched substring. |
$' |
Text on the right of the matched substring. |
$ |
Directly count the symbols. |
Note:Ecmascript V3 stipulates that the replacement parameter of the Replace () method can be a function rather than a string. In this case, each match calls this function, and the string it returns will be used as the replacement text. The first parameter of this function is a matching string. The following parameter is a string that matches the subexpression in the pattern. There can be 0 or more such parameters. The following parameter is an integer that declares the position where the matching occurs in the stringobject. The last parameter is stringobject itself.
String replacement:
VaR STR = "Visit Microsoft! "Document. Write (Str. Replace (/Microsoft/," w3school "))
Global replacement:
VaR STR = "Welcome to Microsoft! "Str = STR +" We are proud to announce that Microsoft has "str = STR +" one of the largest Web developers sites in the world. "document. write (Str. replace (/Microsoft/g, "w3school "))
Returned result: Welcome to w3school! We are proud to announce that w3schoolhas one of the largest Web developers sites in the world.
Use a regular expression:
Name = '"A", "B"'; name. Replace (/"([^"] *) "/g," '$1 '");
Function:
Name = 'aaa bbb ccc '; UW = Name. replace (/\ B \ W + \ B/g, function (Word) {return word. substring (0, 1 ). touppercase () + word. substring (1 );});
4: The Split () method is used to split a string into a string array. Syntax Stringobject. Split (Separator,Howmany)
Parameters |
Description |
Separator |
Required. String or regular expression, which separates stringobject from the specified place. |
Howmany |
Optional. This parameter specifies the maximum length of the returned array. If this parameter is set, no more substrings are returned than the array specified by this parameter. If this parameter is not set, the entire string is split, regardless of its length. |
Return Value
A string array. The array isSeparatorSplit the string stringobject into substrings at the specified boundary. The strings in the returned array do not includeSeparatorItself.
However, ifSeparatorIf it is a regular expression that contains a subexpression, the returned array contains strings that match these subexpressions (but does not include the text that matches the entire regular expression ).
Tips and comments
Note:If the empty string ("") is usedSeparator, Each character in the stringobject will be split.
Note:The operations executed by string. Split () are the opposite to those executed by array. Join.
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 A to uppercase 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 |
Search 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 |
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. |
More