JS:Regex取代

时间:2010-12-30 13:58:42

标签: javascript regex replace

我需要在php中使用javascript。

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

THX

BTW:我试过了

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

但没有发生。

2 个答案:

答案 0 :(得分:2)

从RegExp中删除引号:

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

如果你有引号,那么它试图用''替换字符串'/(\ S)+ \?/ g',因此不用正则表达式替换。

答案 1 :(得分:2)

您需要创建正则表达式对象:

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