fenec
fenec

Reputation: 5806

javascript string manipulation

I would like to modify my strings so i can replace the character ' ' with '_' using JS,for example "new zeland"=>"new_zeland" how can i do that?

Upvotes: -1

Views: 284

Answers (2)

Douwe Maan
Douwe Maan

Reputation: 6878

You could use Rob's code, but it uses a regular expression to find the space, while it would be faster to just search for a literal space:

var string = 'new zealand';
var newString = string.replace(' ', '_');

Upvotes: 1

Rob
Rob

Reputation: 8187

var str = 'new zealand';
str = str.replace(/\s+/g, '_');

Upvotes: 8

Related Questions