1

All -

I am using the below code (got it online) to validate URLs. However the URL "http://www.yahoo.com?srch=news1&Network=[cnn]" fails to return true because of the brackets in URL variable Network. Is it possible to update the regex to include the brackets too, or should I be looping over the URL variables and remove the brackets?

 var webSiteUrlExp = /^(([\w]+:)?\/\/)?(([\d\w]|%[a-fA-f\d]{2,2})+(:([\d\w]|%[a-fA-f\d]{2,2})+)?@)?([\d\w][-\d\w]{0,253}[\d\w]\.)+[\w]{2,4}(:[\d]+)?(\/([-+_~.\d\w]|%[a-fA-f\d]{2,2})*)*(\?(&?([-+_~.\d\w]|%[a-fA-f\d]{2,2})=?)*)?(#([-+_~.\d\w]|%[a-fA-f\d]{2,2})*)?$/;

if (webSiteUrlExp.test(url)) {
      console.log("true");
      return true;
}
else {
     console.log("false");
      return false;
}
fn79
  • 303
  • 6
  • 18
  • The better solution would be to include brackets in the regex. – Cezary Wojcik Apr 23 '13 at 19:21
  • Don't adding "(\[.*\])*" after the "?" at the end of the Regex expression does help ? – RelevantUsername Apr 23 '13 at 19:22
  • Potentially related: http://stackoverflow.com/questions/1547899/which-characters-make-a-url-invalid – ubik Apr 23 '13 at 19:23
  • Where did you find this regex? There's a serious error in the part that matches a hex digit: `[a-fA-f\d]` should be `[a-fA-F\d]`. Even better, you can shorten it to `[a-f\d]` and set the case-insensitive flag. – Alan Moore Apr 23 '13 at 20:11

1 Answers1

1

That's because [ and ] are not valid characters within a URL. The browser will silently convert them to
%5B and %5D respectively, and the server will silently convert them back before passing it to the engine.

If you want to validate URLs with characters outside the normal range, you should first run it through some kind of escaping function.

Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592
  • 1
    Pedantic note: they are not invalid, but rather "reserved" according to RFC3986 ;) – ubik Apr 23 '13 at 19:25
  • Thanks, I will try to update the code to escape the characters – fn79 Apr 23 '13 at 19:31
  • I updated the code to escape the brackets in URL, but does not seem to be working. Here is the link to jsfiddle http://jsfiddle.net/xWeTp/1/ – fn79 Apr 23 '13 at 19:58