URL验证的正则表达式(在JavaScript中) [英] Regular expression for URL validation (in JavaScript)
问题描述
是否有人使用正则表达式来验证网址(不是在文本段落中找到它们)?首选JavaScript代码段。
Does someone have a regex for validating urls (NOT for finding them inside a text passage)? JavaScript snippet would be preferred.
推荐答案
实际的网址语法非常复杂,在正则表达式中难以表示。大多数看起来很简单的正则表达式会产生许多假阴性和误报。欢迎参见这些努力,但即使最终结果也不好。
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.
如今,您通常希望允许IRI以及老式URI,因此我们可以链接到有效地址,例如:
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.
这篇关于URL验证的正则表达式(在JavaScript中)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!