简体   繁体   English

为什么此正则表达式不能在Javascript中使用?

[英]Why isn't this regex working in Javascript?

I currently have this in my script and I am not really sure why it isn't working. 我目前在我的脚本中有此功能,但我不确定为什么它不起作用。 It works on a regex tester and it's quite a simple regular expression. 它可以在正则表达式测试器上运行,并且是一个非常简单的正则表达式。

var page = '<div id="loginOverlay" class="loginOverlay">' +
 '<div id="loginForm">' +
      '<form name="loginForm" method="post" action="/test.jspx" onsubmit="grayLoginAnonymous();return false;" style="margin:0px;" autocomplete="off"><input type="hidden" name="org.apache.struts.taglib.html.TOKEN" value="21a9e5a4197cfaefec409d8473f29a6e" />'+    
     ' </form>'+
  ' </div> '+
' </div>';

var pattern = /<input type='hidden' name='org.apache.struts.taglib.html.TOKEN' value='((\d|\w)+)' \/>/;
var match = page.match(pattern);
document.write(match);
console.log(page);
console.log(match);

</script>

match returns 'null'. 匹配返回“ null”。 Can someone point out the problem? 有人可以指出问题吗?

You have used single quotes instead of double quotes. 您已使用单引号而不是双引号。 Change the pattern to this and it will work: 将模式更改为此,它将起作用:

var pattern = /<input type="hidden" name="org.apache.struts.taglib.html.TOKEN" value="((\d|\w)+)" \/>/;

Also, make sure to take care of the dots like Tim Pietzcker pointed out in his comment! 此外,请务必注意蒂姆·皮茨克(Tim Pietzcker)在其评论中指出的问题!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM