Wednesday, December 26, 2018

Javascript : Replace occurrences of string

The important thing to note is that the replace function is taking regex argument.

var re = /apples/gi;
var str = 'Apples are round, and apples are juicy.';
var newstr = str.replace(re, 'oranges');
console.log(newstr);  // oranges are round, and oranges are juicy.


references:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace

No comments:

Post a Comment