RegExp exec() Method - Javascript RegExp

Javascript examples for RegExp:Method

Description

The exec() method returns the matched text if it finds a match, otherwise it returns null.

Parameter Values

Parameter Description
stringRequired. The string to be searched

Return Value

Type Description
Array An array containing the matched text if it finds a match, otherwise it returns null

The following code shows how to Search a string for the character "e":

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<body>

<button onclick="myFunction()">Test</button>

<p id="demo"></p>

<script>
function myFunction() {//  w  w  w.j  ava  2 s . c o  m
    var str = "The following code shows how to ";
    var patt = new RegExp("e");
    var res = patt.exec(str);
    document.getElementById("demo").innerHTML = res;
}
</script>

</body>
</html>

Related Tutorials