caner taşdemir caner taşdemir - 1 year ago 87
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' \/>");
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download