I’m working on a HTML5 input pattern polyfill and I’m trying to validate an input type=url in JavaScript exactly as the browser (Chrome) does but can’t find any documentation on a JavaScript or PERL compatible regular expression. As it’s a polyfill, I don’t particularly mind if it matches all URL’s exactly (which is impossible) but rather that it imitates how the browser works.
Would anyone know of an identical pattern in PERL syntax?
Thanks
Read the regarding specification at http://www.w3.org/TR/html5/forms.html#url-state-(type=url):
Your polyfill should start with sanitizing the input, i.e. removing linebreaks and trimming the string. The sentence "User agents must not allow users to insert "LF" (U+000A) or "CR" (U+000D) characters" might also be interesting.
The results should be a valid, absolute URL. The there referenced RFCs 3986 and 3987 will be describing the URL validation, the section about parsing URLs may be as well interesting.
Your polyfill might not only validate URIs, it also may resolve relative URIs. At least, validating a URI will be much simpler with an algortihm instead of finding an appropriate regexp. Yet, even the RFC mentions a regexp for parsing a already validated URI in appendix B.