域 = www.example.xx | example.xx
我需要使用这些规则的正则表达式查询:
- 符合世界域名规则的有效域名。
- 连续不包含 2 个点。
- 不是 IP\Ftp。
- 最后:xxxx 不允许
- 在 . 之后的域名(.com 或 .xx.xx)不能少于 2 位数字。
我已经尝试使用此正则表达式组合输入类型='url':
(?=(^[^.]+(?:\.[^.]+)+$))(?=([-a-zA-Z0-9@:%_\+.~#?&//=]{2,256}\.[a-z]{2,4}\b(\/[-a-zA-Z0-9@:%_\+.~#?&//=]*)?))
但它打破了我添加 http:/
或 http:
或添加 www.
不是 2 位数字规则...
最佳答案
测试以下 RegEx 模式:
{
let pattern = /^[^\s]+?(?!\d+)[^.\s]+?(?<!\.[a-zA-Z]{2,2})\.(?!com)([a-zA-Z]{2,3}$)/g;
function test( string ) {
console.log( string.match( pattern ) );
}
test( `example.org` );
test( `www.example.net` );
test( `www.example.nz` );
test( `12.33.29.24` );
test( `example.nz` );
test( `example.com` );
test( `example.co.kr` );
}
关于javascript - 具有独特规则的 url 正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49998503/