-1
我是新来的正则表达式。任何人都可以指向或建议一个正则表达式来验证一个完全合格的域名(如so - mysite.com)或URL验证器正则表达式,而不需要协议检查吗?Angular 2中的FQDN/URL格式验证器正则表达式
我是新来的正则表达式。任何人都可以指向或建议一个正则表达式来验证一个完全合格的域名(如so - mysite.com)或URL验证器正则表达式,而不需要协议检查吗?Angular 2中的FQDN/URL格式验证器正则表达式
这应该工作:
正则表达式:
/^([a-z0-9-]+\.[a-z0-9]{2,}$)/gm
输入:
mysite.com
test.net
testing.-com
camels.com.net
输出:
mysite.com
test.net
的JavaScript代码:
const regex = /^([a-z0-9-]+\.[a-z0-9]{2,}$)/gm;
const str = `mysite.com
test.net
testing.-com
camels.com.net`;
let m;
while ((m = regex.exec(str)) !== null) {
// This is necessary to avoid infinite loops with zero-width matches
if (m.index === regex.lastIndex) {
regex.lastIndex++;
}
// The result can be accessed through the `m`-variable.
m.forEach((match, groupIndex) => {
console.log(`Found match, group ${groupIndex}: ${match}`);
});
}
/^[a-zA-Z0-9][a-zA-Z0-9-]{1,61}[a-zA-Z0-9]\.[a-zA-Z]{2,}$/