Javascript正则表达式替换[0]而不是0

时间:2016-08-17 07:45:08

标签: javascript html regex

我想在我的HTML中替换[0]的每个实例,但我现在使用的正则表达式现在替换所有0而不仅仅是[0]。这就是我所拥有的:

var clone = clone.html().replace(/[0]/g, total); // Total is a number that increments

我要替换的一个例子是:

<input id="Items[0].Id" name="Items[0].Id" value="0" type="text" />

但是上面的Javascript取代了0中的值=“0”,任何我想要落实的想法?

1 个答案:

答案 0 :(得分:4)

你需要逃离开头的方括号:

var clone = clone.html().replace(/\[0]/g, total);

如果你这样做([0]),它将被视为只包含字符0的{​​{3}}。