使用javascript替换字符串的所有出现

时间:2014-02-27 12:49:38

标签: javascript regex string replace

我想用单引号替换所有出现的字符串,但是str.replace它只替换第一次出现的脚本:

"7<singleQuote>1 inche<singleQuote>s"

代码

var data = "7<singleQuote>1 inche<singleQuote>s"
var output = data.replace("<singleQuote>","'")

输出:7'1 inche<singleQuote>s

我想将<singleQuote>替换为'

1 个答案:

答案 0 :(得分:2)

将正则表达式与g标志一起使用:

var output = data.replace(/<singleQuote>/g, "'");

MDN: String.prototype.replace