To search a string for a text that matches RegExp, use rhe exec() method in JavaScript. If it finds a match, it returns an array of results; otherwise, it returns null.
The following is the parameter −
- string − The string to be searched
Example
You can try to run the following code to search a string for text matching RegExp −
<html>
<head>
<title>JavaScript RegExp exec Method</title>
</head>
<body>
<script>
var str = "JavaScript is an interesting scripting language";
var re = new RegExp( "script", "g" );
var result = re.exec(str);
document.write("Test 1 - returned value : " + result);
re = new RegExp( "pushing", "g" );
var result = re.exec(str);
document.write("<br />Test 2 - returned value : " + result);
</script>
</body>
</html>