Jump to content

Regexp Problem.


Cod-nes
 Share

Recommended Posts

I don't know why isn't my code not working.

var str="doamin.com/index.php?something=bah";var patt=new RegExp("?");var pat=new RegExp("&");document.write("Test if we find " + patt + " in " + str + ": ")if (patt.test(str)==true){document.write("Match found!");}else if(pat.test(str)==true){document.write("Match found!");}else{document.write("Match not found");}

Link to comment
Share on other sites

I don't know why isn't my code not working.
var str="doamin.com/index.php?something=bah";var patt=new RegExp("?");var pat=new RegExp("&");document.write("Test if we find " + patt + " in " + str + ": ")if (patt.test(str)==true){document.write("Match found!");}else if(pat.test(str)==true){document.write("Match found!");}else{document.write("Match not found");}

"?" is a reserved character in regular expressions. I also recommend not using the RegExp() constructor because it can lead to confusion (you need extra backslashes to escape things.Try this:
var patt =  /\?/;var pat = /&/;

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...