简体   繁体   English

如何将网址与正则表达式匹配?

[英]How do I match URLs with regular expressions?

We want to check if a URL matches mail.google.com or mail.yahoo.com (also a subdomain of them is accepted) but not a URL which contains this string after a question mark. 我们要检查URL是否匹配mail.google.com或mail.yahoo.com(也接受它们的子域),但不检查在问号后包含此字符串的URL。 We also want the strings "mail.google.com" and "mail.yahoo.com" to come before the third slash of the URL, for example https://mail.google.com/ is accepted, https://www.facebook.com/mail.google.com/ is not accepted, and https://www.facebook.com/?mail=https://mail.google.com/ is also not accepted. 我们也希望字符串“mail.google.com”和“mail.yahoo.com”来的URL的第三个斜杠前,例如https://mail.google.com/被接受, https://www.facebook.com/mail.google.com/不被接受,也不接受https://www.facebook.com/?mail=https://mail.google.com/ https://mail.google.com.au/ is also not accepted. 也不接受https://mail.google.com.au/ Is it possible to do it with regular expressions? 是否可以使用正则表达式呢?

var possibleURLs = /^[^\?]*(mail\.google\.com|mail\.yahoo\.com)\//gi;
var url;
// assign a value to var url.
if (url.match(possibleURLs) !== null) {
    // Do something...
}

Currently this will match both https://mail.google.com/ and https://www.facebook.com/mail.google.com/ , but we don't want to match https://www.facebook.com/mail.google.com/ . 目前,这将同时匹配https://mail.google.com/https://www.facebook.com/mail.google.com/ ,但是我们不想匹配https://www.facebook.com/mail.google.com/

Edit : I want to match any protocol (any string which doesn't contain "?" and "/" ) followed by a slash "/" twice (the string and the slash can both be twice), then any string which doesn't contain "?" 编辑 :我想匹配任何协议(不包含"?""/"任何字符串),然后匹配两次斜杠"/" (该字符串和斜杠都可以是两次),然后匹配任何不包含'不能包含"?" and "/" (if it's not empty, it must end with a dot "." ), and then (mail\\.google\\.com|mail\\.yahoo\\.com)\\/ . "/" (如果不为空,则必须以点"."结尾),然后是(mail\\.google\\.com|mail\\.yahoo\\.com)\\/ Case insensitive. 不区分大小写。

Not being funny - but why must it be a regular expression? 不好笑-但是为什么一定要使用正则表达式?

Is there are reason why you couldn't simplify the process using URL (or webkitURL in Chrome and Safari) - the URL constructor simply takes a string and then contains properties for each part of the URL. 是否有原因为什么您不能使用URL (或Chrome和Safari中的webkitURL)简化该过程-URL构造函数只接受一个字符串,然后包含URL的每个部分的属性。 Whether it supports all the host types that you want to support, I don't know. 我不知道它是否支持您要支持的所有主机类型。

Granted, you might still need a regex after that (although really you'd just be checking that the hostname ends with either yahoo.com or google.com), but you would just be running it against the hostname of the URL object rather than the whole URI. 当然,在那之后您可能仍需要一个正则表达式(尽管实际上您只是在检查主机名以yahoo.com或google.com结尾),但是您只是针对URL对象的主机名而不是运行它整个URI。

The API is not ubiquitous, but seems reasonably well supported and, anyway, if this is client-side validation then I hope you're checking it on the server, too, because sidestepping javascript validation is easy. 该API并非无处不在,但是似乎得到了很好的支持,无论如何,如果这是客户端验证,那么我希望您也在服务器上对其进行检查,因为回避JavaScript验证很容易。

^([-a-z]+://|^cid:|^//)([^/\?]+\.)?mail\.(google|yahoo)\.com/

Should do the trick 应该做的把戏

The first ^ means "match beginning of line", the second negates the allowed characters, thus making a slash / not allowed. 第一个^表示“匹配行首”,第二个取反允许的字符,因此使用斜杠/不允许。

Nb. 铌。 You still have to escape the slashes, or use it as a string in new RegExp(string) : 您仍然必须转义斜线,或将其用作new RegExp(string)

new RegExp('^([-a-z]+://|^cid:|^//)([^/\?]+\.)?mail\.(google|yahoo)\.com/')

How about 怎么样

^[a-z]+:\/\/([^.\/]+\.)*mail\.(google|yahoo).com\/

Regex Example Link 正则表达式示例链接

  • ^ Anchors the regex at the start of the string ^在字符串开头锚定正则表达式

  • [az]+ Matches the protocol. [az]+匹配协议。 If you want a specific set of protocols, then (https?|ftp) may do the work 如果您需要一组特定的协议,则(https?|ftp)可以完成工作

  • ([^.\\/]+\\.)* matches the subdomin part ([^.\\/]+\\.)*与次要部分匹配

好的,我发现它适用于:

var possibleURLs = /^([^\/\?]*\/){2}([^\.\/\?]+\.)*(mail\.google\.com|mail\.yahoo\.com)\//gi;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM