用于验证的 javascript 中 1-10 的正则表达式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6168314/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 19:44:07  来源:igfitidea点击:

Regex for 1-10 in javascript for validation

javascriptregex

提问by Hriskesh Ashokan

What would be the regex for numbers ranging 1-10 and 1-5? Please help this troubled soul.

1-10 和 1-5 之间的数字的正则表达式是什么?请帮助这个陷入困境的灵魂。

回答by marsbear

You could achive that with easy number checks in javascript:

您可以通过 javascript 中的简单数字检查来实现:

// Convert input to integer just to be sure
mynum = parseInt(mynum, 10);

// Check number-range
if(mynum >= 1 && mynum <=10)
and
if(mynum >= 1 && mynum <=5)

If you really want to use regex:

如果你真的想使用正则表达式:

/^([1-9]|10)$/
and
/^[1-5]$/

UPDATE:

更新

  • Fixed the first regex to correctly match the string boundings
  • Added parseInt to the first example to ensure correct number-checks
  • 修复了第一个正则表达式以正确匹配字符串边界
  • 将 parseInt 添加到第一个示例以确保正确的数字检查

回答by Jason McCreary

This is not a good use of Regular Expressions.

这不是正则表达式的一个很好的用途。

Use simple conditions:

使用简单条件:

if (x > 0 && x < 6) {
  // x is 1 - 5
}

if (x > 0 && x < 10) {
  // x is 1 - 10
}

回答by mario

For 1-5 you only need to enclose it as character class:

对于 1-5,您只需要将其作为字符类括起来:

  /^[1-5]$/

For 1-10 you'd just need an additional alternative:

对于 1-10,您只需要一个额外的选择:

  /^([1-9]|10)$/

回答by Tim Cooper

Is there a reason you want to use regular expressions?

你有什么理由想要使用正则表达式吗?

/([1-9]|10)/

回答by KooiInc

Use numeric comparison. The following Number extension can check if a number falls between 2 values:

使用数字比较。下面的 Number 扩展可以检查一个数字是否在 2 个值之间:

Number.prototype.between = 
  function(lower,upper, includeBoundaries){
    lower = Number(lower);
    upper = Number(upper);
    noCando = isNaN(lower) || 
              isNaN(upper) || 
              lower>=upper;
    if ( noCando ) {
      throw 'wrong arguments or out of range';
    }
    return includeBoundaries
           ? this >= lower && this <= upper
           : this > lower && this < upper
};
// usage:
(12).between(1,12); /=> false
(12).between(1,12,true); /=> true
(12).between(0,15,true); /=> true
(0).between(-5,1); //=> true

The function converts the parameters to Number because 0 can evaluate to a boolean in javascript, to be able to check if the paramaters are real number values and to be able to check if lower is not greater than/equal to upper. In those cases an error is thrown.

该函数将参数转换为数字,因为 0 可以在 javascript 中计算为布尔值,以便能够检查参数是否为实数值,并能够检查下限是否不大于/等于上限。在这些情况下,会抛出错误。

The includeBoundariesparameter also checks if a Number is equal to lower or upper, if it's not supplied, the function returns a real 'between'-check.

includeBoundaries参数还检查数字是否等于下限或上限,如果未提供,则该函数返回一个真正的“介于”检查。

回答by Artūras Stifanovi?ius

The answer would be

答案是

/^([1-9]|10)$/

回答by Howard

For 1-10 it can be

对于 1-10 可以是

/^([1-9]|10)$/

and for 1-5 simply

而对于 1-5 只是

/^[1-5]$/