1

I would like to replace every instance of [0] in my HTML but the regex I am using now replaces all 0s not just [0]. Here's what I have:

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

An example of what I would replace is something like:

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

But the above Javascript replaces the 0 in value="0", any ideas where I am falling through?

Morgs
  • 1,558
  • 3
  • 19
  • 34

1 Answers1

4

You will need to escape the opening square bracket:

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

If you leave it like that ([0]) it will be treated like a character class consisting of only the character 0.

Cristian Lupascu
  • 39,078
  • 16
  • 100
  • 137