Home > Article > Web Front-end > What does javascript match mean?
javascript match is a method used to retrieve a specified value within a string, or find a match for one or more regular expressions. This method is similar to indexOf() and lastIndexOf(), but it returns The specified value, not the position of the string.
The operating environment of this article: windows7 system, javascript version 1.8.5, Dell G3 computer.
match definition and usage
The match() method retrieves a specified value within a string, or finds a match for one or more regular expressions.
This method is similar to indexOf() and lastIndexOf(), but it returns the specified value instead of the position of the string.
Syntax
stringObject.match(searchvalue) stringObject.match(regexp)
Parameters
searchvalue Required. Specifies the string value to retrieve.
regexp Required. A RegExp object that specifies the pattern to match. If the parameter is not a RegExp object, you need to first pass it to the RegExp constructor to convert it to a RegExp object.
Return value
An array storing matching results. The contents of this array depend on whether regexp has the global flag g.
Description
The match() method will retrieve the string stringObject to find one or more texts that match the regexp. The behavior of this method depends heavily on whether regexp has flag g.
If regexp does not have the flag g, then the match() method can only perform one match in stringObject. If no matching text is found, match() returns null. Otherwise, it returns an array with information about the matching text it found. The 0th element of the array holds the matching text, while the remaining elements hold the text that matches the regular expression's subexpressions. In addition to these regular array elements, the returned array contains two object properties. The index attribute declares the position of the starting character of the matching text in stringObject, and the input attribute declares a reference to stringObject.
If the regexp has flag g, the match() method will perform a global search and find all 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 content of the array returned by global matching is very different from the former. Its array elements store all matching substrings in stringObject, and there is no index attribute or input attribute.
Note: In global search mode, match() does not provide information about the text matched by the subexpression, nor does it declare the position of each matching substring. If you need this globally retrieved information, you can use RegExp.exec().
[Recommended learning: js basic tutorial]
Example
Example 1
In this example, we will perform different searches in "Hello world!":
<script type="text/javascript"> var str="Hello world!" document.write(str.match("world") + "<br />") document.write(str.match("World") + "<br />") document.write(str.match("worlld") + "<br />") document.write(str.match("world!")) </script>
Output:
world null null world!
Example 2
In this example we will use a globally matched regular expression to retrieve all numbers in a string:
<script type="text/javascript"> var str="1 plus 2 equal 3" document.write(str.match(/\d+/g)) </script>
Output:
1,2,3
The above is the detailed content of What does javascript match mean?. For more information, please follow other related articles on the PHP Chinese website!