V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
b824379598
V2EX  ›  问与答

关于js正则匹配域名的问题

  •  
  •   b824379598 · 2013-07-31 15:33:39 +08:00 · 5429 次点击
    这是一个创建于 3915 天前的主题,其中的信息可能已经有所发展或是发生改变。
    需求是正则匹配的域名可以包含*?可以出现在任意位置,任意次数
    网上找的一个正则匹配,看js看的头都大了。

    var name = /[a-zA-Z0-9][-a-zA-Z0-9]{0,62}(\.[a-zA-Z0-9][-a-zA-Z0-9]{0,62})+\.?/;

    if( !(name.test(domain)) || (domain.substr(0,1) == ".") || (domain.substr(domain.length-1,domain.length) == "-"))
    {
    alert("请填写合法的域名!");
    $('#domain').focus();
    return false;
    }

    可结果确变成这样

    可以配置成功的域名包括:

    *.?.com;*.??c.com;*.a?c.com;*.ab?.com;*.?bc.com;*.????????bc.com

    不可以配置的域名包括:

    *.??.com;*.?b?.com;*.???.com;*.????????bc?.com
    目前尚无回复
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2782 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 13:02 · PVG 21:02 · LAX 06:02 · JFK 09:02
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.