So I have a line of code in which I want to replace all specific codes with a an image. I have it working fine with strings but when I try to enter a code I have issues.
$('#wrapper').html($('#wrapper').html().replace(/(0)/g, '<img src="/Content/Images/mana/0.png" />'));
If the (0) was a word, it would work fine, but it's replacing every single 0 in the document. Is there a way to force it to recognise the brackets as part of the string? I tried enclosing them in quotation marks but I have the same problems.
Thanks in advance
asked Dec 17, 2015 at 11:38
scottdavidwalker
1,6311 gold badge20 silver badges35 bronze badges
1 Answer 1
You need to escape the brackets.
$('#wrapper').html($('#wrapper').html().replace(/\(0\)/g, '<img src="/Content/Images/mana/0.png" />'));
answered Dec 17, 2015 at 11:42
Rich
5,7519 gold badges45 silver badges63 bronze badges
Sign up to request clarification or add additional context in comments.
1 Comment
scottdavidwalker
Thanks !!!!!!!!!!!!!!!!!!!!!! I'll accept the answer in 8 minutes because SO is being daft :-)
default
.replace(/\(0\)/g???