caner taşdemir caner taşdemir - 8 months ago 51
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 ?


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

Make it

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