ES6 - RegExp search()


Advertisements

This method returns the index where the match was found in the string. If no match is found, it returns -1.

Syntax

str.replace(regexp|substr, newSubStr|function)           

Parameter Details

  • Regexp − A regular expression object.

  • Substr − String to be replaced.

  • newSubStr − The replacement string.

  • function − the function to create a new string.

Return Value

Returns the index at which the match was found in the string.

Example

var str = 'Welcome to ES6.We are learning ES6'; 
var re = new RegExp(/We/); 
var found = str.search(re); 
console.log(found); 

Output

0     
es6_regexp.htm
Advertisements