ES6 - RegExp replace()



This method returns a new string after replacing the matched pattern.

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

A new string after replacing all matches.

Example

var str = 'Good Morning'; 
var newstr = str.replace('Morning', 'Night'); 
console.log(newstr);  

Output

Good Night     
Advertisements