jquery如何替换所有字符串

时间:2014-12-05 13:42:52

标签: javascript jquery

我在javascript中有字符串,例如:"我有两台PC和两台笔记本电脑&#34 ;; 结果应该是"我有三台PC和三台笔记本电脑"。

所以,我想改变所有出现的单词" 2"到"三"

3 个答案:

答案 0 :(得分:0)

new_string =' pop' .replace(/ two / g,' three')

这将改变两个'与'三'

答案 1 :(得分:0)

我将尝试解释,替换和正则表达式可以替换多个事件。

var input = "i have two PC and two laptops";
var output = input.replace(/two/g, "three");

答案 2 :(得分:0)

这将使他们全部得到:

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));