nagmanat
nagmanat

Reputation: 29

JS: Regex in replace

i need this in php to javascript.

echo preg_replace('/(\S)+\?/', '', 'http://example.com/?test=1');

THX

BTW: I tried

alert('http://example.com/?test=1'.replace('/(\S)+\?/g', ''));

but no happens.

Upvotes: 0

Views: 172

Answers (2)

strager
strager

Reputation: 90012

You need to create a regular expression object:

alert('http://example.com/?test=1'.replace(/(\S)+\?/g, ''));

Upvotes: 2

Damiqib
Damiqib

Reputation: 1077

Remove quotes from your RegExp:

alert('http://example.com/?test=1'.replace(/(\S)+\?/g, ''));

If you have quotes there, then it's trying to replace the string '/(\S)+\?/g' with '', and therefore not doing regular expression replace.

Upvotes: 2

Related Questions