views:

638

answers:

2

Does someone have a regex for validating urls (NOT for finding them inside a text passage)? JavaScript snippet would be preferred.

A: 

Try this regex, it works for me.

ennuikiller
I'd like to be sure that I will not get false negatives with this regex. You had not problems with it?
Immortal
No problems as of yet. Why not conduct tests on the false positives your anticipating?
ennuikiller
It *would* give a false negative for symbols like ';' in the query, *except* that that's a great big `\S+` in the middle of the expression which can expand to match nearly anything, and it's not anchored at the end so you can put any trailing nonsense in. eg. ‘http://@’ or ‘http://I've got a lovely bunch of "coconuts"’ are ‘valid’.
bobince
+1  A: 

The actual URL syntax is pretty complicated and not easy to represent in regex. Most of the simple-looking regexes out there will give many false negatives as well as false positives. See for amusement these efforts but even the end result is not good.

Plus these days you would generally want to allow IRI as well as old-school URI, so we can link to valid addresses like:

http://en.wikipedia.org/wiki/Þ
http://例え.テスト/

I would go only for simple checks: does it start with a known-good method: name? Is it free of spaces and double-quotes? If so then hell, it's probably good enough.

bobince
Ok. At least now I know it's probably not worth the effort in most cases :] Thanks.
Immortal