jpalladino84
jpalladino84

Reputation: 2510

Javascript RegEx global string search for underscore character(_)

I am horrible with RegEx and I have been using this online tester for some time now and still can not find what I need.

So I have the string "2011_G-20_Cannes_summit". I want to replace all the underscores (_) with spaces.

So I want something like this:

var str = "2011_G-20_Cannes_summit";
str.replace(/_/g," "); or str.replace(/\_/g);

Though neither is working...

What am I missing?

Upvotes: 1

Views: 816

Answers (1)

That works fine. The replace method doesn't modify the existing string, it creates a new one. This will do what you want:

var str = "2011_G-20_Cannes_summit";
str = str.replace(/_/g," ");

Upvotes: 2

Related Questions