ES6 - RegExp exec()



The exec method searches string for text that matches regexp. If it finds a match, it returns an array of results; otherwise, it returns null.

Syntax

RegExpObject.exec( string );         

Parameter Details

  • String − The string to be searched

Return Value

It returns the matched text if a match is found, and NULL if not.

Example

var str = "Javascript is an interesting scripting language"; 
var re = new RegExp( "script", "g" ); 
var result = re.exec(str); 
console.log("Test 1 - returned value : " +  result);  
re = new RegExp( "pushing", "g" ); 
var result = re.exec(str); 
console.log("Test 2 - returned value : " +  result)

Output

Test 1 - returned value : script 
Test 2 - returned value : null      
Advertisements