javascript match() method
Translation results:
英[mætʃ] 美[mætʃ]
n.Competition; match; opponent; matching person (or thing)
vt. Same; adapt; compete; make equal
vt.& vi. To match, to be commensurate
Third person singular: matches Plural: matches Present participle: matching Past tense: matched Past participle: matched
javascript match() methodsyntax
Function: You can 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 rather than 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: 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 the regexp does not have the g flag, then the match() method can only perform a match on the 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 performs a global search, finding 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().
javascript match() methodexample
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <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> </body> </html>
Run instance »
Click the "Run instance" button to view the online instance