JavaScript exec () method
Definition and Usage
exec () method is used to retrieve the string matches the regular expression.
If the string matching the value of returns that match the value, otherwise return null.
grammar
RegExpObject.exec(string)
参数 | 描述 |
---|---|
string | Required. The string to be searched |
Browser Support
All major browsers support exec () method
Examples
Examples
In the global search for the string "Hello" and "W3CSchool" string:
<script>
var str="Hello world!";
//look for "Hello"
var patt=/Hello/g;
var result=patt.exec(str);
document.write("Returned value: " + result);
//look for "W3Schools"
patt=/W3Schools/g;
result=patt.exec(str);
document.write("<br>Returned value: " + result);
</script>
var str="Hello world!";
//look for "Hello"
var patt=/Hello/g;
var result=patt.exec(str);
document.write("Returned value: " + result);
//look for "W3Schools"
patt=/W3Schools/g;
result=patt.exec(str);
document.write("<br>Returned value: " + result);
</script>
Examples of the above output:
Returned value: Hello
Returned value: null
Returned value: null
try it"
JavaScript RegExp Object