开发者

Why isn't this regex working in Javascript?

开发者 https://www.devze.com 2023-01-29 16:27 出处:网络
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.

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'. 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!

0

精彩评论

暂无评论...
验证码 换一张
取 消