0

I'm working with Jquery, and I wrote this code for replace all string variable. But not working.

var s = ":) :) :)".replace(new RegExp(':)','g'),'<img src="../images/smiley.gif" />');
alert(s);

How can i solve this problem? Thanks

1 Answer 1

1

You must escape the parenthesis :

new RegExp(':\\)','g')

As you can see, there are two \ : one because the ) must be escaped in a regex, and one because a \ must be escaped in a string literal.

It's simpler to use a regular expression literal :

var s = ":) :) :)".replace(/:\)/g,'<img src="../images/smiley.gif" />');
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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.