URL 验证的正则表达式(在 JavaScript 中) [英] Regular expression for URL validation (in JavaScript)

查看:24
本文介绍了URL 验证的正则表达式(在 JavaScript 中)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人有用于验证网址的正则表达式(不是用于在文本段落中找到它们)?最好使用 JavaScript 代码段.

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

推荐答案

实际的 URL 语法相当复杂,并且不容易用正则表达式表示.大多数看起来简单的正则表达式都会给出许多假阴性和假阳性.查看娱乐这些努力但即使最终结果也不好.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆