Prologue
Every time I need to use JavaScript there’s this tiny voice in the back of my head “Please don’t”, for instance because of
JavaScript has two sets of equality operators:
===
and!==
, and their evil twins==
and!=
.
Verify a URI in JavaScript with a Regular Expression using Google Search examples
This time it did it again: I used JavaScript. My need was to verify a basic URI in JavaScript, so I wrote this function based on RFC 3986 [WayBack] which in Appendix B has a nice regular expression: ^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?
function isValidUri(uri){ var uriRegExPattern = "^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?"; var uriRegEx = new RegExp(uriRegExPattern); return (uriRegEx.test(uri)); }
It would crash. But JavaScript is JavaScript, so even a site like JSFiddle wouldn’t show an error (later I found out that enabling the console on http://jsbin.com/wamavacuco/edit?html,console,output does show the error in the console complete with stack trace).