简体   繁体   English

不包含连续字符的正则表达式

[英]Regex for not containing consecutive characters

I can't figure out javascript regex that would satisfy all those requirements:我想不出满足所有这些要求的 javascript 正则表达式:

The string can only contain underscores and alphanumeric characters.字符串只能包含下划线和字母数字字符。 It must begin with a letter, not include spaces, not end with an underscore, and not contain two consecutive underscores.它必须以字母开头,不能包含空格,不能以下划线结尾,并且不能包含两个连续的下划线。

This is as far as I came, but 'not containing consecutive underscores' part is the hardest to add.这是就我而言,但“不包含连续下划线”部分是最难添加的。

^[a-zA-Z][a-zA-Z0-9_]+[a-zA-Z0-9]$

You could use multiple lookaheads (neg. ones in this case):您可以使用多个前瞻(在这种情况下为否定):

^(?!.*__)(?!.*_$)[A-Za-z]\w*$

See a demo on regex101.com .在 regex101.com 上查看演示


Broken down this says: 分解这说:

 ^ # start of the line (?!.*__) # neg. lookahead, no two consecutive underscores (edit 5/31/20: removed extra Kleene star) (?!.*_$) # not an underscore right at the end [A-Za-z]\\w* # letter, followed by 0+ alphanumeric characters $ # the end


As JavaScript snippet: 作为JavaScript片段:

 let strings = ['somestring', '_not_this_one', 'thisone_', 'neither this one', 'but_this_one', 'this__one_not', 'this_one__yes'] var re = /^(?!.*__)(?!.*_$)[A-Za-z]\\w*$/; strings.forEach(function(string) { console.log(re.test(string)); });

Please do not restrain passwords!请不要限制密码!

You can also use你也可以使用

^[a-zA-Z]([a-zA-Z0-9]|(_(?!_)))+[a-zA-Z0-9]$

Demo演示

The only change comparing to your regex is changing [a-zA-Z0-9_] to [a-zA-Z0-9]|(_(?!_)) .与您的正则表达式相比,唯一的变化是将[a-zA-Z0-9_]更改为[a-zA-Z0-9]|(_(?!_)) I removed underscore from the character set and allow it in the second part of the alternative if it's not followed by another one.我从字符集中删除了下划线,如果后面没有另一个下划线,则允许它出现在备选方案的第二部分。

(?!_) is negative lookahead meaning that _ cannot be next character (?!_)是负前瞻意味着_不能是下一个字符

See regex in use here请参阅此处使用的正则表达式

^[a-z](?!\w*__)(?:\w*[^\W_])?$
  • ^ Assert position as the start of the line ^断言位置作为行的开始
  • [az] Match any lowercase ASCII letter. [az]匹配任何小写 ASCII 字母。 The code below adds the i (case-insensitive) flag, thus this also matches the uppercase variables下面的代码添加了i (不区分大小写)标志,因此这也匹配大写变量
  • (?!\\w*__) Negative lookahead ensuring two underscores do not exist in the string (?!\\w*__)负前瞻确保字符串中不存在两个下划线
  • (?:\\w*[^\\W_])? Optionally match the following可选匹配以下内容
    • \\w* Match any number of word characters \\w*匹配任意数量的单词字符
    • [^\\W_] Match any word character except _ . [^\\W_]匹配除_之外的任何单词字符。 Explained: Match anything that is not not a word character, but not _ (since it's in the negated set).解释:匹配任何 不是 单词字符但不是_ (因为它在否定集中)。
  • $ Assert position at the end of the line $断言行尾位置

 let a = ['somestring', '_not_this_one', 'thisone_', 'neither this one', 'but_this_one', 'this__one_not', 'this_one__yes'] var r = /^[az](?!\\w*__)(?:\\w*[^\\W_])?$/i a.forEach(function(s) { if(r.test(s)) console.log(s) });

甚至更简单的版本,没有环视(因此也可用于不支持它们的正则表达式风格,例如 POSIX ERE,甚至sed风格的正则表达式,只需简单更改):

^[a-zA-Z](_?[a-zA-Z0-9]+)*$

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM