_.replace

returns a new string with some or all matches of a pattern replaced by a replacement

  1. // Lodash
  2. var re = /apples/gi;
  3. var str = 'Apples are round, and apples are juicy.';
  4. var newstr = _.replace(str, re, 'oranges');
  5. console.log(newstr);
  6. // output: 'oranges are round, and oranges are juicy.'
  7.  
  8. // Native
  9. var re = /apples/gi;
  10. var str = 'Apples are round, and apples are juicy.';
  11. var result = str.replace(re, 'oranges');
  12. console.log(result);
  13. // output: 'oranges are round, and oranges are juicy.'

Browser Support for String.prototype.replace()

ChromeEdgeFirefoxIEOperaSafari
1.0 ✔