javascript正则表达式验证,强制使用字母字符或字母数字字符,但不强制使用独立数字

时间:2012-07-13 08:52:07

标签: javascript regex

寻找验证这两种情况的Javascript验证正则表达式

  • 字符或字符+数字
  • 没有独立号码

谢谢,

4 个答案:

答案 0 :(得分:0)

尝试使用此正则表达式^[\da-zA-Z]*?[a-zA-Z]+[\da-zA-Z]*?$

答案 1 :(得分: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');
  }
}

答案 2 :(得分: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)

答案 3 :(得分: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]
方括号

(?=a way to say AND[^0-9]仅排除数字,[a-zA-Z0-9]仅允许字母,字母+数字。