Jump to content

Regexp Problem.


Cod-nes

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...