1

これらのケースの両方を検証する Javascript 検証正規表現を探しています

  • 文字 OR 文字 + 数字
  • スタンドアロン番号なし

ありがとう、

4

4 に答える 4

0
/(?=[^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]文字のみを許可し、文字 + 数字を許可します。

于 2012-07-13T09:03:26.657 に答える
0

この正規表現を使用してみてください^[\da-zA-Z]*?[a-zA-Z]+[\da-zA-Z]*?$

于 2012-07-13T08:55:46.620 に答える
0

これはどう?

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');
  }
}
于 2012-07-13T08:56:36.807 に答える
0

これを試して

(?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)
于 2012-07-13T08:58:13.793 に答える