Grammar
Stringobject.match (Searchvalue)
Stringobject.match (RegExp)
<script type= "text/web Effects" >
var str= "Hello world!"
document.write (Str.match ("World") + "<br/>")//world
document.write (Str.match ("World") + "<br/>")//null
document.write (Str.match ("worlld") + "<br/>")//null
document.write (Str.match ("world!"))//world!
</script>
The following example shows the use of the Match function method in JS:
function Matchdemo () {
var r, re; Declare a variable.
var s = "The rain in Spain falls mainly in the plain";
re =/ain/i; Creates a regular expression pattern.
r = S.match (re); Try to match the search string.
return (R); Return to the place where "Ain" first appeared.
}
This example shows the use of the match function method with the G flag setting in JS
function Matchdemo () {
var r, re; Declare a variable.
var s = "The rain in Spain falls mainly in the plain";
re =/ain/ig; Creates a regular expression pattern.
r = S.match (re); Try to match the search string.
return (R); The returned array contains all the "Ain"
Four matches that occurred.
}
Note: In global retrieval mode, match () does not provide information about the text that matches the subexpression, nor does it declare the position of each matching substring. If you need information for these global searches, you can use Regexp.exec ().