0

I'm sure somebody will be able to help me on this; I'm trying to replace :) with the word smiley - The following works but only replaces the first string:

var string = "hello :)";

string = string.replace(":)", "smiley");

I tried this but it won't work:

var string = "hello :)";

string = string.replace(/:)/g, "smiley");

Any ideas?

1 Answer 1

2

You're on the right track with your second example, but the /.../g notation creates a regular expression, and ) has a special meaning in a regular expression; you need to "quote" or "escape" it with a backslash:

string = string.replace(/:\)/g, "smiley");
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.