sridhar
sridhar

Reputation: 1389

Replacing + with plus using javascript

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?

Upvotes: 1

Views: 80

Answers (2)

Matus Dubrava
Matus Dubrava

Reputation: 14462

+ 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"));

Upvotes: 1

prasanth
prasanth

Reputation: 22490

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)

Upvotes: 5

Related Questions