Latest web development tutorials

JavaScript exec () method

RegExp Object Reference JavaScript RegExp Object

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

Internet ExplorerFirefoxOperaGoogle ChromeSafari

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>

Examples of the above output:

Returned value: Hello
Returned value: null

try it"


RegExp Object Reference JavaScript RegExp Object