I have string in javascript, for example : "i have two PC and two laptops"; and result should be "i have three PC and three laptop".
So, I want to change all occurrences of word "two" to "three"
I have string in javascript, for example : "i have two PC and two laptops"; and result should be "i have three PC and three laptop".
So, I want to change all occurrences of word "two" to "three"
new_string = 'pop'.replace(/two/g,'three')
this will change 'two' with 'three'
I will try to explain, replacng more then one occurence can be done with replace and a regular expression.
var input = "i have two PC and two laptops";
var output = input.replace(/two/g, "three");
This will get them all:
function replace(find, replace, str) {
return str.replace(new RegExp(find, 'g'), replace);
}
var str = 'i have two PC and two laptops';
var find = 'two';
var replace = 'three'
alert(replaceAll(find,replace,str));