[英]Regex to allow sub domains
Following regex working fine cases where domain is root domain, but this regex not recognizing sub domains as valid domains: 在regex可以正常工作的情况下,domain是根域,但是此regex无法将子域识别为有效域:
For example: abc.com
is being recognized as valid domain, but if I use ab.bc.com
then regex says string is not valid, how can I improve this to supprt ab.bc.com
too? 例如:
abc.com
被识别为有效域,但是如果我使用ab.bc.com
则正则表达式说字符串无效,我该如何改善它以支持ab.bc.com
呢?
var reg = /^[a-zA-Z0-9][a-zA-Z0-9-]{1,61}[a-zA-Z0-9]\.[a-zA-Z]{2,}$/;
if ( !reg.test(the_domain))
{
alert('has special characters');
}
else
{
alert('no special characters');
}
You can add a quantifier for the xx. 您可以为xx添加一个量词。 pattern, and make it as
/^([a-zA-Z0-9][a-zA-Z0-9-]{0,61}[a-zA-Z0-9]\\.)+[a-zA-Z]{2,}$/
模式,并将其设置为
/^([a-zA-Z0-9][a-zA-Z0-9-]{0,61}[a-zA-Z0-9]\\.)+[a-zA-Z]{2,}$/
var reg = /^([a-zA-Z0-9][a-zA-Z0-9-]{0,61}[a-zA-Z0-9]\\.)+[a-zA-Z]{2,}$/; // ^ ^ changes made console.log(reg.test("abc.com")); console.log(reg.test("ab.bc.com")); console.log(reg.test("ab.bc.com")); console.log(reg.test("ab-.bc.com"));
/^(\\S+\\.\\S+)$/
This makes sure it has at least a word with a period and another word, if it repeats then its fine. 这样可以确保它至少包含一个带句点的单词和另一个单词,如果重复则可以。
https://jsfiddle.net/3yb1v222/ https://jsfiddle.net/3yb1v222/
or to be more restrictive on characters you can just do: 或对字符进行更严格的限制,您可以执行以下操作:
/^[A-Za-z0-9-_&\\?]+(\\.[A-Za-z0-9-_&\\?]+){1,2}/
you can remove the additional special characters if you are excluding the path on any given url, but either way... its the same concept. 如果要排除任何给定url上的路径,则可以删除其他特殊字符,但无论哪种方式……都是相同的概念。
https://jsfiddle.net/u7hgabec/ https://jsfiddle.net/u7hgabec/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.