V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
b824379598
V2EX  ›  问与答

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

  •  
  •   b824379598 · Jul 31, 2013 · 6185 views
    This topic created in 4653 days ago, the information mentioned may be changed or developed.
    需求是正则匹配的域名可以包含*?可以出现在任意位置,任意次数
    网上找的一个正则匹配,看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
    No Comments Yet
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   1002 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 23ms · UTC 22:23 · PVG 06:23 · LAX 15:23 · JFK 18:23
    ♥ Do have faith in what you're doing.