Angular 2中的FQDN / URL格式验证器正则表达式

时间:2017-02-22 16:28:44

标签: regex angular

我是regex的新手。任何人都可以指向或建议使用正则表达式来验证完全限定的域名(如此 - mysite.com)或没有协议检查的url验证器正则表达式吗?

1 个答案:

答案 0 :(得分:0)

这应该有效:

正则表达式:

/^([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}`);
    });
}




请参阅:https://regex101.com/r/ncVEAT/3