これらのケースの両方を検証する Javascript 検証正規表現を探しています
- 文字 OR 文字 + 数字
- スタンドアロン番号なし
ありがとう、
/(?=[^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]
文字のみを許可し、文字 + 数字を許可します。
この正規表現を使用してみてください^[\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)