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

标签 javascript regex

域 = 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/

相关文章:

html - 否定词与正则表达式的匹配

用于国家域的 php 正则表达式

javascript - Keystone JS : localhost is not in the certs altnames

javascript - 在类上添加 onclick 事件

javascript - javascript 中有没有一种方法可以使用年份和 ISO 周数创建日期对象?

regex - .htaccess 重写所有字符

c# - 使用 ffmpeg 和正则表达式获取 'location' 的视频

javascript - node.js 和 mysql 如果数据库中有 1,我如何获得 2?

javascript - PDF Js 在 meteor 中不起作用

javascript - 如何使用正则表达式 javascript 匹配一串单词