寻找验证这两种情况的 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 [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');
}
}
试试这个
(?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)^([a-z0-9]*[a-z][a-z0-9]*)$
解释
(?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)
/(?=[^0-9][a-zA-Z0-9])/
是你的神奇正则表达式。
[ 'ads', '3ds', '3' ].map( function( c ) {
return /(?=[^0-9][a-zA-Z0-9])/.test( c );
});
[true, true, false]
(?=
是方括号的一种说法AND
。[^0-9]
仅排除数字、仅[a-zA-Z0-9]
允许字母和字母 + 数字。