1

I have a text that contains:

var str = 'Coins ++';

I need to replace it with 'Coins plusplus'

The following doesn't work:

str = str.replace(/+/g , "plus");

How to get this done?

3

2 Answers 2

5

use with \ before the plus. because + is one of the special character of the regex

var str = 'Coins ++';

str = str.replace(/\+/g, "plus");
console.log(str)

Sign up to request clarification or add additional context in comments.

Comments

1

+ is a special character when used inside of regex, you need to escape it by adding \ in front of it to make regex recognize it as a string.

var str = 'Coins ++';

console.log(str.replace(/\+/g, "plus"));

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.