javascript正则表达式验证,强制使用字母字符或字母数字字符,但不使用独立数字
正在寻找验证这两种情况的Javascript验证正则表达式javascript正则表达式验证,强制使用字母字符或字母数字字符,但不使用独立数字,javascript,regex,Javascript,Regex,正在寻找验证这两种情况的Javascript验证正则表达式 字符或字符+数字 没有独立的号码 谢谢,尝试使用这个正则表达式^[\da-zA-Z]*?[a-zA-Z]+[\da-zA-Z]*?$这个怎么样 var tests = [ 'fsdfdsfASAS34csdfsd', 'dadsd', '332' ]; // add here whatever you like to test var re = /^(?=.*[a-z])[0-9a-z]+$/i; // with
- 字符或字符+数字
- 没有独立的号码
谢谢,尝试使用这个正则表达式
^[\da-zA-Z]*?[a-zA-Z]+[\da-zA-Z]*?$
这个怎么样
var tests = [
'fsdfdsfASAS34csdfsd',
'dadsd',
'332'
]; // add here whatever you like to test
var re = /^(?=.*[a-z])[0-9a-z]+$/i;
// with [0-9a-z]+ we test that string contains only alphanumericals,
// and with (?=.*[a-z]) we test that it has at least one [a-zA-Z] character present
for (var i = 0, l = tests.length; i < l; ++i) {
if (re.test(tests[i])) {
console.log(tests[i] + ' passed');
}
else {
console.log(tests[i] + ' failed');
}
}
var测试=[
“fsdfdsfASAS34csdfsd”,
"爸爸",,
'332'
]; // 在这里添加您想要测试的内容
变量re=/^(?=.[a-z])[0-9a-z]+$/i;
//使用[0-9a-z]+我们测试字符串只包含字母数字,
//使用(?=.*[a-z])我们测试它是否至少存在一个[a-zA-z]字符
对于(变量i=0,l=tests.length;i
试试这个
(?i)\b([a-z0-9]*[a-z][a-z0-9]*)\b
解释
(?i) # Match the remainder of the regex with the options: case insensitive (i)
\b # Assert position at a word boundary
( # Match the regular expression below and capture its match into backreference number 1
[a-z0-9] # Match a single character present in the list below
# A character in the range between “a” and “z”
# A character in the range between “0” and “9”
* # Between zero and unlimited times, as many times as possible, giving back as needed (greedy)
[a-z] # Match a single character in the range between “a” and “z”
[a-z0-9] # Match a single character present in the list below
# A character in the range between “a” and “z”
# A character in the range between “0” and “9”
* # Between zero and unlimited times, as many times as possible, giving back as needed (greedy)
)
\b # Assert position at a word boundary
(?is) # Match the remainder of the regex with the options: case insensitive (i); dot matches newline (s)
^ # Assert position at the beginning of the string
( # Match the regular expression below and capture its match into backreference number 1
[a-z0-9] # Match a single character present in the list below
# A character in the range between “a” and “z”
# A character in the range between “0” and “9”
* # Between zero and unlimited times, as many times as possible, giving back as needed (greedy)
[a-z] # Match a single character in the range between “a” and “z”
[a-z0-9] # Match a single character present in the list below
# A character in the range between “a” and “z”
# A character in the range between “0” and “9”
* # Between zero and unlimited times, as many times as possible, giving back as needed (greedy)
)
$ # Assert position at the end of the string (or before the line break at the end of the string, if any)
或
解释
(?i) # Match the remainder of the regex with the options: case insensitive (i)
\b # Assert position at a word boundary
( # Match the regular expression below and capture its match into backreference number 1
[a-z0-9] # Match a single character present in the list below
# A character in the range between “a” and “z”
# A character in the range between “0” and “9”
* # Between zero and unlimited times, as many times as possible, giving back as needed (greedy)
[a-z] # Match a single character in the range between “a” and “z”
[a-z0-9] # Match a single character present in the list below
# A character in the range between “a” and “z”
# A character in the range between “0” and “9”
* # Between zero and unlimited times, as many times as possible, giving back as needed (greedy)
)
\b # Assert position at a word boundary
(?is) # Match the remainder of the regex with the options: case insensitive (i); dot matches newline (s)
^ # Assert position at the beginning of the string
( # Match the regular expression below and capture its match into backreference number 1
[a-z0-9] # Match a single character present in the list below
# A character in the range between “a” and “z”
# A character in the range between “0” and “9”
* # Between zero and unlimited times, as many times as possible, giving back as needed (greedy)
[a-z] # Match a single character in the range between “a” and “z”
[a-z0-9] # Match a single character present in the list below
# A character in the range between “a” and “z”
# A character in the range between “0” and “9”
* # Between zero and unlimited times, as many times as possible, giving back as needed (greedy)
)
$ # Assert position at the end of the string (or before the line break at the end of the string, if any)
这是你的魔法正则表达式
[ 'ads', '3ds', '3' ].map( function( c ) {
return /(?=[^0-9][a-zA-Z0-9])/.test( c );
});
[true, true, false]
(?=
用于方括号。[^0-9]
仅排除数字,[a-zA-Z0-9]
只允许字母和字母+数字。我正在尝试使用关键代码,但我对regex不太熟悉,我想使用它,因为简单是一个很好的起点。\w
也会匹配\u
。这不是吗?FFS,伙计,为什么不在控制台中测试它呢?它不是向后看,而是向前看,仅供参考。