Learn the most commonly used test exec match search replace split 6 methods
Definitions and usage
The test () method is used to detect whether a string matches a pattern.
Grammar
Regexpobject.test (String) parameter description
String required. The string to be instrumented.
return value
Returns True if string strings contain text that matches regexpobject, or false.
Description
Call the test () method of the RegExp object R and pass the string s to it, which is equivalent to this expression: (R.exec (s)!= null).
1 Test checks to see if the specified string exists
The code is as follows |
Copy Code |
var data = "123123"; var recat =/123/gi; Alert (recat.test (data)); True
|
Check to see if the character is present G continue to go down I case insensitive
2 Exec return query value the EXEC () method retrieves the matching of regular expressions in a string.
The code is as follows |
Copy Code |
var data = "123123,213,12312,312,3,cat,cat,dsfsdfs,"; var recat =/cat/i; Alert (recat.exec (data)); Cat |
Description
The exec () method is powerful, it is a common method, and is more complex to use than the test () method and the method that supports the String object of the regular expression.
If EXEC () finds a matching text, an array of results is returned. Otherwise, NULL is returned. The No. 0 element of this array is the text that matches the regular expression. The 1th element is the text (if any) that matches the 1th subexpression of Regexpobject, and the 2nd element is the text (if any) that matches the 2nd subexpression of Regexpobject, Push. In addition to the array elements and length properties, the Exec () method returns two properties. The Index property declares the position of the first character of the matching text. The input property holds a string that is retrieved. We can see that when calling the Exec () method of a Non-global RegExp object, the returned array is the same as the array returned by the calling Method String.match ().
However, when Regexpobject is a global regular expression, the exec () behavior is slightly more complex. It starts retrieving string strings at the character specified by the Regexpobject lastindex property. When exec () finds the text that matches the expression, after the match, it sets the Regexpobject lastindex property to match the next position of the last character in the text. This means that you can iterate through all the matching text in a string by repeatedly calling the Exec () method. When exec () can no longer find the matching text, it returns null and resets the Lastindex property to 0.
3 Match Gets the query array
The match () method retrieves the specified value within a string, or finds a match for one or more regular expressions.
The method is similar to IndexOf () and LastIndexOf (), but it returns the specified value, not the position of the string.
code is as follows |
copy code |
& nbsp; var data = "123123,213,12312,312,3,cat,cat,dsfsdfs,"; var recat =/cat/gi; var arrmactches = Data.match (recat) for (Var i=0;i < arrmactches.length i++) { alert (arrmactches[i]); //cat Cat } |
The match () method retrieves the string stringobject to find one or more text that matches the regexp. The behavior of this method depends to a great extent on whether the regexp has a sign G.
If RegExp does not have a flag G, then the match () method can only perform a match in Stringobject. If no matching text is found, match () returns NULL. Otherwise, it returns an array that holds information about the matching text it finds. The No. 0 element of the array holds the matching text, while the remaining elements hold the text that matches the subexpression of the regular expression. In addition to these regular array elements, the returned array also contains two object properties. The Index property declares the position of the starting character of the matching text in the Stringobject, and the input property declares a reference to the Stringobject.
If the regexp has a flag G, the match () method performs a global search and finds all the matching substrings in the stringobject. If no matching substring is found, NULL is returned. If one or more matching substrings are found, an array is returned. However, the contents of the array returned by the global match are very different from the former, and its array elements contain all the matching substrings in the stringobject, and there is no index attribute or input property.
4 Search to return to a location similar to IndexOf
The search () method retrieves the substring specified in the string, or retrieves a substring that matches the regular expression.
The code is as follows |
Copy Code |
var data = "123123,213,12312,312,3,cat,cat,dsfsdfs,"; var recat =/cat/gi; Alert (Data.search (Recat)); 23 |
Description
The search () method does not perform a global match, and it ignores flag g. It ignores the RegExp lastindex property at the same time and always retrieves it from the beginning of the string, which means it always returns the first matching position of Stringobject.
5 Replace replacement character using regular substitution
The replace () method replaces some characters in a string with some other characters, or replaces a substring that matches a regular expression.
Grammar
Stringobject.replace (regexp/substr,replacement)
Cases
The code is as follows |
Copy Code |
var data = "123123,213,12312,312,3,cat,cat,dsfsdfs,"; var recat =/cat/gi; Alert (Data.replace (Recat, "libinqq")); |
The replace () method of the string Stringobject performs a find-and-replace operation. It will look for substrings in the stringobject that match the regexp, and then replace them with replacement. If RegExp has global flag G, then the Replace () method replaces all matching substrings. Otherwise, it replaces only the first matching substring.
Replacement can be either a string or a function. If it is a string, then each match is replaced by a string. However, the $ character in replacement has a specific meaning. As shown in the following table, it shows that the string that is matched from the pattern will be used for substitution.
6 The Split method is used to split a string into an array of strings.
The code is as follows |
Copy Code |
var data = "123123,213,12312,312,3,cat,cat,dsfsdfs,"; var recat =/,/; var arrdata = Data.split (recat); for (var i = 0; i < arrdata.length; i++) { Alert (Arrdata[i]); } |
return value
An array of strings. The array is created by splitting the string stringobject into substrings at the boundary specified by separator. The string in the returned array does not include the separator itself.
However, if separator is a regular expression that contains a subexpression, the returned array includes a string that matches the subexpression (but does not include text that matches the entire regular expression).
Tips and comments
Note: If an empty string ("") is used as a separator, then each character in the Stringobject is split.
Note: The action performed by String.Split () is the opposite of what Array.join does
3 Learning under the simple class negative class range class combination class
The code is as follows |
Copy Code |
Simple class var data = "1LIBINQQ,2LIBINQQ,3LIBINQQ,4LIBINQQ"; var recat =/[123]libinqq/gi; var arrdata = Data.match (recat); for (var i = 0; i < arrdata.length; i++) { Alert (Arrdata[i]); 1LIBINQQ 2LIBINQQ 3LIBINQQ } Negative to Class var data = "ALIBINQQ,1LIBINQQ,2LIBINQQ,3LIBINQQ,4LIBINQQ"; U0062cf var recat =/[^a123]libinqq/gi; var arrdata = Data.match (recat); for (var i = 0; i < arrdata.length; i++) { Alert (Arrdata[i]); 4libinqq } Scope class var data = "Libinqq1,libinqq2,libinqq3,libinqq4,libinqq5"; U0062cf var recat =/libinqq[2-3]/gi; var arrdata = Data.match (recat); for (var i = 0; i < arrdata.length; i++) { Alert (Arrdata[i]); LIBINQQ2 libinqq3 } Combination Class var data = "a,b,c,w,1,2,3,5"; U0062cf var recat =/[a-q1-4n]/gi; var arrdata = Data.match (recat); for (var i = 0; i < arrdata.length i++) & nbsp { alert (arrdata[i]); //a B C 1 2 3 & nbsp; } |