用于验证带有子域的 url 的正则表达式 [英] regular expression for validating url with sub-domain

查看:47
本文介绍了用于验证带有子域的 url 的正则表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

preg_match('/^(http:\\/\/)?(.+)\.MYDOMAIN\.com(.*)/', $url, $match)

这是我的正则表达式,用于验证必须具有子域的 URL,但如果有人使用 www而不是子域,它也得到验证.例如:

This is my regex to validate a URL that must have a sub-domain, but if someone uses www instead of sub-domain it also gets validated. For example:

  • http://shoes.en.MYDOMAIN.com/ 这个必须通过

http://www.MYDOMAIN.com/ 这一定会失败

如果子域是 www,我如何编辑我的正则表达式失败?

How can I edit my regex to fail if the sub-domain is www?

推荐答案

我认为您可以使用否定前瞻断言来做到这一点:(?!www\.) 放置在协议检查之后, 检查开始或协议后面是否没有 www. .

I think you can do it with a negative lookahead assertion: (?!www\.) which being placed after the protocol check, checks if there is not a www. following the start or protocol.

/^(http:\\\/\/)?(?!www\.)(.+)\.MYDOMAIN\.com(.*)/.test("www.MYDOMAIN.com")

这篇关于用于验证带有子域的 url 的正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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