JavaScript equivalent for PHP preg_replace
javascripts string.replace function also takes a regular expression:
"test test test test".replace(/ +/,'-');
http://jsfiddle.net/5yn4s/
In JavaScript, you would write it as:
result = subject.replace(/ +/g, "-");
By the way, are you sure you've posted the right PHP code? It would rather be:
$result = preg_replace('/ +/', '-', $str);
var text = 'test test test test';
var fixed = text.replace(/\s+/g, '-');