caner taşdemir caner taşdemir - 1 month ago 4
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

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

Make it

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