1

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
3
  • please provide jsfiddle for that Commented Dec 17, 2015 at 11:40
  • 1
    .replace(/\(0\)/g ??? Commented Dec 17, 2015 at 11:41
  • you use {{0}} intend of (0) in your html. Commented Dec 17, 2015 at 11:41

1 Answer 1

3

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
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks !!!!!!!!!!!!!!!!!!!!!! I'll accept the answer in 8 minutes because SO is being daft :-)

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.