Friday, September 13, 2013

How to replace multiple occurence in a string in javascript

Here is the code :
str = str.replace(/abc/g, '');
 
or you can use this one :
var find = 'abcxyz';
var re = new RegExp(find, 'g');
str = str.replace(re, ''); 

or

function replaceAll(find, replace, str) {
  return str.replace(new RegExp(find, 'g'), replace);
}
 
call the function :  
replaceAll(find, replace, str);