Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regular expression to replace spaces with dashes

I'm trying to work out what regular expression I would need to take a value and replace spaces with a dash (in Javascript)?

So say if I had North America, it would return me North-America ?

Can I do something like var foo = bar.replace(' ', '-') ?

like image 894
Rich Avatar asked Dec 19 '09 09:12

Rich


1 Answers

It's better to use:

var string = "find this and find that".replace(/find/g, "found");

to replace all occurrences.

like image 103
Alexander Shvetsov Avatar answered Oct 25 '22 22:10

Alexander Shvetsov