具有独特规则的url正则表达式

时间:2018-04-24 09:49:48

标签: javascript regex

domain = 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位数规则......

1 个答案:

答案 0 :(得分:1)

按照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` );
}
&#13;
&#13;
&#13;