我无法弄清楚满足所有这些要求的javascript正则表达式:
该字符串只能包含下划线和字母数字字符.它必须以字母开头,不包括空格,不以下划线结尾,并且不包含两个连续的下划线.
这是我的意思,但"不包含连续下划线"部分是最难添加的.
^[a-zA-Z][a-zA-Z0-9_]+[a-zA-Z0-9]$
Run Code Online (Sandbox Code Playgroud)
您可以使用多个前瞻(在这种情况下为neg.):
^(?!.*__)(?!.*_$)[A-Za-z]\w*$
Run Code Online (Sandbox Code Playgroud)
请参阅regex101.com上的演示.
^ # start of the line
(?!.**__) # neg. lookahead, no two consecutive underscores
(?!.*_$) # not an underscore right at the end
[A-Za-z]\w* # letter, followed by 0+ alphanumeric characters
$ # the end
Run Code Online (Sandbox Code Playgroud)
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));
});Run Code Online (Sandbox Code Playgroud)
请不要限制密码!
| 归档时间: |
|
| 查看次数: |
3307 次 |
| 最近记录: |