0

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?

3
  • 1
    I think it's because you are trying to match that pattern exactly, when var page contains characters before and after that pattern. Try putting a .* at the beginning and end. You may also need to specify matching across new lines. Commented Dec 8, 2010 at 10:35
  • 1
    Be aware that . means "any character" in regular expressions. So while it will also match a literal dot, you should be more specific and write \. if you want a regex to match an actual dot. Commented Dec 8, 2010 at 10:35
  • It was just a mistake on my part about the double quote and single quote. Thanks guys. Commented Dec 8, 2010 at 11:19

1 Answer 1

4

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!

Sign up to request clarification or add additional context in comments.

1 Comment

oh. my. god. what a dumb mistake. Thanks Jakob.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.