Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript equivalent for PHP preg_replace

I've been looking for a js-equivalent for the PHP preg_replace function and what I found so far is simply string.replace.

However I'm not sure how to convert my regular expression to JavaScript. This is my PHP code:

preg_replace("/( )*/", $str, $str); 

So for example the following:

test   test   test test 

becomes:

test-test-test-test 

Anyone knows how I can do this in JavaScript?

like image 752
Skyfe Avatar asked Aug 16 '11 13:08

Skyfe


1 Answers

var text = 'test   test   test test'; var fixed = text.replace(/\s+/g, '-'); 
like image 127
ant_Ti Avatar answered Sep 21 '22 23:09

ant_Ti