正则表达式无效量词?

时间:2016-10-24 17:44:41

标签: javascript regex

我正在使用带有JavaScript的正则表达式而且我遇到了一个让我回复Invalid quantifier ?的问题。

这是代码:

if(url.match(new RegExp('^https?://.+/foresee(?:-|_)surveydef\.js(?:\?)(.*)','g')) != null)

我在其他问题上阅读了我需要在\字符之前添加?,但它无法修复它...

if(url.match(new RegExp('^https\?://.+/foresee(\?:-|_)surveydef\.js(\?:\?)(.*)','g')) != null)

输入网址为http://marvel.com/,我只是检查匹配。

1 个答案:

答案 0 :(得分:0)

使用以下代码修复它:

if(url.match(new RegExp('^https?://.+/foresee(?:-|_)surveydef\.js(\\?:\?)(.*)','g'))