Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 正则表达式只允许字母、数字、点、下划线和破折号。至少5个字符_Javascript_Regex - Fatal编程技术网

Javascript 正则表达式只允许字母、数字、点、下划线和破折号。至少5个字符

Javascript 正则表达式只允许字母、数字、点、下划线和破折号。至少5个字符,javascript,regex,Javascript,Regex,如何使正则表达式符合以下规则 仅允许字母(大写或小写)、数字、点、下划线、破折号 至少5个字符 不能包含通用术语或扩展名(例如:.com、.net)。php、.mustache、.html、.js、.jpeg、.jpg、.png、.tiff、… e、 g. username.comx username.commeo 这是我的,如果包含点、下划线、破折号,则匹配失败, 以及如何排除扩展字符串 ^[a-zA-Z0-9.-]*\w{5,}$ 可能是这样的: ^([\w.-](?!\.(com|net|

如何使正则表达式符合以下规则

  • 仅允许字母(大写或小写)、数字、点、下划线、破折号
  • 至少5个字符
  • 不能包含通用术语或扩展名(例如:.com、.net)。php、.mustache、.html、.js、.jpeg、.jpg、.png、.tiff、…
    e、 g.
    username.com
    x
    username.comme
    o
  • 这是我的,如果包含点、下划线、破折号,则匹配失败,
    以及如何排除扩展字符串
    ^[a-zA-Z0-9.-]*\w{5,}$


    可能是这样的:

    ^([\w.-](?!\.(com|net|html?|js|jpe?g|png)$)){5,}$
    
    解释:

    ^              # from start            
    ([\w.-]        # \w is equal to [a-zA-Z0-9_]
        (?!\.          # in front can NOT be a dot followed by
            (com           # com
            |net           # OR net
            |html?         # OR htm or html     # ? means optional match
            |js            # OR js
            |jpe?g         # OR jpg or jpeg
            |png           # OR png
            )$             # block only if it is at the end
        )              # end of the negative lookahead
    ){5,}          # match at least 5 characters in above conditions
    $              # till the end
    

    希望能有所帮助。

    应该是这样的:

    ^([\w.-](?!\.(com|net|html?|js|jpe?g|png)$)){5,}$
    
    解释:

    ^              # from start            
    ([\w.-]        # \w is equal to [a-zA-Z0-9_]
        (?!\.          # in front can NOT be a dot followed by
            (com           # com
            |net           # OR net
            |html?         # OR htm or html     # ? means optional match
            |js            # OR js
            |jpe?g         # OR jpg or jpeg
            |png           # OR png
            )$             # block only if it is at the end
        )              # end of the negative lookahead
    ){5,}          # match at least 5 characters in above conditions
    $              # till the end
    
    希望它能有所帮助。

    尽管您可以(可能)将它合并到一个regexp中,但它的性能会非常差。。。您最好使用一个函数,它可以使用两个 正则表达式

    第二部分可能更适合使用数组检查,将所有值拆分为一个数组

    function isValid(str) {
      return (/^([\w\d_\.]{5,})$/i).test(str) 
        && !(/\.(dll|com|net|exe|php|html|js|jpeg|jpg|png|tiff|gif)$/i).test(str);
    }
    
    虽然您可以(可能)将它合并到一个regexp中,但它的性能会非常差。。。您最好使用一个函数,它可以使用两个 正则表达式

    第二部分可能更适合使用数组检查,将所有值拆分为一个数组

    function isValid(str) {
      return (/^([\w\d_\.]{5,})$/i).test(str) 
        && !(/\.(dll|com|net|exe|php|html|js|jpeg|jpg|png|tiff|gif)$/i).test(str);
    }
    


    example.com
    可接受吗?不包含
    .com
    则您的第三条规则不清楚..谢谢回答,我在问题中更新了
    example.com
    可接受吗?不包含
    .com
    则您的第三条规则不清楚..谢谢回答,我更新了InQuestionUp投票,以获得每一步的详细解释,并展示OP的魔力。感谢回复,我粘贴到
    https://regex101.com/r/pT0iD8/3
    但是如果
    username.comme
    无法通过,这不是OP想要的
    example.come
    是可以接受的,但是@user1575921。当我第一次读你的问题时,我没有看到第三条规则,很抱歉。。。我已经updated@WashingtonGuedes非常感谢!成功了!谢谢你的详细解释!!向上投票,查看每一步的详细说明,并展示OP的魔力。感谢回复,我粘贴到
    https://regex101.com/r/pT0iD8/3
    但是如果
    username.comme
    无法通过,这不是OP想要的
    example.come
    是可以接受的,但是@user1575921。当我第一次读你的问题时,我没有看到第三条规则,很抱歉。。。我已经updated@WashingtonGuedes非常感谢!成功了!谢谢你的详细解释@JS中的PM77-1 look aheads速度特别慢,更难理解内联。。。虽然一般来说,正则表达式很难让人理解。。。此外,检查结尾字符的通用扩展名应该与允许的字符检查分开,这实际上是两条规则。@JS中的PM77-1 look aheads特别慢,更难理解内联。。。虽然一般来说,正则表达式很难让人理解。。。此外,检查结尾字符的通用扩展名应该与允许的字符检查分开,这实际上是两条规则。