caner taşdemir caner taşdemir - 10 months ago 58
jQuery Question

jQuery - Why it replaces the value twice?

I need to convert ":)" to smile image in the string. Used this;

var post = "hello :)";
post = post.replace(/[:)]/g, "<img src='smile.png' \>");

However, the output is

Hello <img src='smile.png' \> <img src='smile.png' \>

How can I prevent it being replacing incorrectly ?

Answer Source

It is matching both : and ) and replacing them one by one

Make it

post = post.replace(/(:\))/g, "<img src='smile.png' \/>");