admin 管理员组

文章数量: 1086019

How to make regex fit below rules

  1. allow only letters (uppercase or smallcase), numbers, dot, underscore, dash
  2. at least 5 characters
  3. can't contain generic terms or extensions (ex: , ). .php, .mustache, .html, .js, .jpeg, .jpg, .png, .tiff, ..
    e.g.
    username x
    usernameme o

here's mine, fail to match if contain dot, underscore, dash,
and how to exclude extension string
^[a-zA-Z0-9_.-]*\w{5,}$

How to make regex fit below rules

  1. allow only letters (uppercase or smallcase), numbers, dot, underscore, dash
  2. at least 5 characters
  3. can't contain generic terms or extensions (ex: ., ). .php, .mustache, .html, .js, .jpeg, .jpg, .png, .tiff, ..
    e.g.
    username. x
    username.me o

here's mine, fail to match if contain dot, underscore, dash,
and how to exclude extension string
^[a-zA-Z0-9_.-]*\w{5,}$

https://regex101./r/pT0iD8/1

Share edited Mar 17, 2016 at 0:02 user1575921 asked Mar 16, 2016 at 23:54 user1575921user1575921 1,0881 gold badge18 silver badges29 bronze badges 4
  • Is example. acceptable? – Shafizadeh Commented Mar 16, 2016 at 23:59
  • no it contains . – user1575921 Commented Mar 17, 2016 at 0:00
  • 1 Then your third rule is unclear .. – Shafizadeh Commented Mar 17, 2016 at 0:02
  • thanks for reply, I update in question – user1575921 Commented Mar 17, 2016 at 0:03
Add a ment  | 

2 Answers 2

Reset to default 10

It would be something like this:

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

Explaining:

^              # from start            
([\w.-]        # \w is equal to [a-zA-Z0-9_]
    (?!\.          # in front can NOT be a dot followed by
        (           # 
        |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

Hope it helps.

Though you could (possibly) wrangle it into a single regexp, it would perform really badly... you'd be much better off using a function, that can use two regular expressions.

The second part may be better off with an array check, with all the values split into an array.

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

本文标签:

Error[2]: Invalid argument supplied for foreach(), File: /www/wwwroot/roclinux.cn/tmp/view_template_quzhiwa_htm_read.htm, Line: 58
File: /www/wwwroot/roclinux.cn/tmp/route_read.php, Line: 205, include(/www/wwwroot/roclinux.cn/tmp/view_template_quzhiwa_htm_read.htm)
File: /www/wwwroot/roclinux.cn/tmp/index.inc.php, Line: 129, include(/www/wwwroot/roclinux.cn/tmp/route_read.php)
File: /www/wwwroot/roclinux.cn/index.php, Line: 29, include(/www/wwwroot/roclinux.cn/tmp/index.inc.php)