Javascript正则表达式帮助初学者

时间:2009-08-05 12:09:49

标签: javascript regex

我需要通过javascript中的regex验证以下范围。

-20.00至+20.00,增量为0.5。

用户应该能够输入1.50而不是+01.50。

我试图在谷歌做很多研究,虽然正则表达并不容易。如果我理解正确,那么我需要将正则表达式分成更小的组。从程序员的角度来看,我认为,我需要验证键入输入字段的每个“char”。如果我理解分组如何在正则表达式中工作,那么我想我可以自己做正则表达式。所以请帮助我理解上述问题的正则表达式,不要只是将正则表达式抛入:)

谢谢,

7 个答案:

答案 0 :(得分:4)

使用正则表达式验证数值是没有意义的。

尝试:

function checkRange(input) {
    var value = parseFloat(input);
    return (!isNaN(value) && (value >= -20) && (value <= 20) && (value % 0.5 == 0));
}

答案 1 :(得分:1)

以下应该这样做

var myregexp = /^[+-]?(20\.00|[01?][0-9]\.[05]0)$/m;
if (subject.match(myregexp)) {
    // Successful match
} else {
    // Match attempt failed
}

// ^[+-]?(20\.00|[01?][0-9]\.[05]0)$
// 
// Options: ^ and $ match at line breaks
// 
// Assert position at the beginning of a line (at beginning of the string or after a line break character) «^»
// Match a single character present in the list below «[+-]?»
//    Between zero and one times, as many times as possible, giving back as needed (greedy) «?»
//    The character “+” «+»
//    The character “-” «-»
// Match the regular expression below and capture its match into backreference number 1 «(20\.00|[01?][0-9]\.[05]0)»
//    Match either the regular expression below (attempting the next alternative only if this one fails) «20\.00»
//       Match the characters “20” literally «20»
//       Match the character “.” literally «\.»
//       Match the characters “00” literally «00»
//    Or match regular expression number 2 below (the entire group fails if this one fails to match) «[01?][0-9]\.[05]0»
//       Match a single character present in the list “01?” «[01?]»
//       Match a single character in the range between “0” and “9” «[0-9]»
//       Match the character “.” literally «\.»
//       Match a single character present in the list “05” «[05]»
//       Match the character “0” literally «0»
// Assert position at the end of a line (at the end of the string or before a line break character) «$»

答案 2 :(得分:0)

可能是检查你有两个有效数字浮点数的简单方法(正则表达式是好的,但也可以使用parseFloat),最后一步检查这些数字是否在[-20.0 - 20.0]范围内

答案 3 :(得分:0)

你想要的正则表达式是:

/[-+]?([01]?[0-9](\.[05]0?)?|(20(\.00?)?)/

这分为:

[-+]?  : an optional - or +
( 
 [01]?  : an optional 0 or
 [0-9]  : number 0-9
 (...)? : Optional
  \.    : .
  [05]  : 0 or 5
  0?    : Optional 0

  |      : OR

  20     : numbers 20
  (...)? : Optional
   \.    : .
   00?   : O or OO
)

请注意,这将允许“-00.00”

的奇数角情况

答案 4 :(得分:0)

这应该分两步完成。第一步是使用正则表达式来确保输入的值在文本上是有效的(即,它具有x个十进制数,它有一个符号等。)然后将其转换为值以确保它在逻辑上是有效的。

首先,您可能需要这样的正则表达式:

/^[+-]?\d{1,2}\.(00|50)$/

它会:

  • 确保字符串可选+-开头。
  • 后跟一两个号码。
  • 后跟一个圆点。
  • 后跟0050

之后,您可以确保它在-20到20之间:

var floatValue = Number(value);
if (floatValue < -20 || floatValue > 20) {
    // Invalid!
}

答案 5 :(得分:0)

如上所述,使用数字可以更好地匹配范围,但使用正则表达式可确保小数点后的格式正确。

var value = ... // eg. "-20.05"
var number = parseFloat(value);
if (number >= -20 && number <= 20 && /[+-]?\d+\.[05]0/.test(value)) {
   alert("in range")
} else {
   alert("out of range")
}

答案 6 :(得分:0)

以下正则表达式应该有效:

^[+-]?(?:20\.?0{0,2}?|[01]?[0-9]\.?[05]?0?)$

说明:

^                             # anchor the match at the start of the string
   [+-]?                      # optional sign
   (?:                        # non-capturing group for alternation:
      20\.?0{0,2}?            # match either 20 (optionally followed by . or .0 or .00)
   |                          # or
      [01]?[0-9]\.?[05]?0?    # match 00-19 (optionally followed by . or .0 or .5 or .50 or .00)
   )                          # end of alternation
$                             # anchor the match at the end of the string