JS: Regex in replace
i need this in php to javascript.
echo p开发者_StackOverflow中文版reg_replace('/(\S)+\?/', '', 'http://example.com/?test=1');
THX
BTW: I tried
alert('http://example.com/?test=1'.replace('/(\S)+\?/g', ''));
but no happens.
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.
You need to create a regular expression object:
alert('http://example.com/?test=1'.replace(/(\S)+\?/g, ''));
精彩评论