问题描述
我想知道是否可以在中使用
语句正则表达式。我想检查一个值是否在可接受的范围内。如果是,则执行其他操作。
I wonder if it possible to use in if
statement regex. I want to check if a value is in an accepted range. If yes to do something otherwise something else.
范围 0.001-0.009
。我知道如何使用多个 if
来检查这个,但我想知道是否有办法在单个中检查它是否
带正则表达式的声明。
The range is 0.001-0.009
. I know how to use multiple if
to check this, but I want to know if there is any way to check it in a single if
statement with regex.
推荐答案
你问的是关于数字比较的问题,所以正则表达式真的没什么用处理这个问题。您不需要多个 if
语句来执行此操作:
You're asking a question about numeric comparisons, so regular expressions really have nothing to do with the issue. You don't need "multiple if
" statements to do it, either:
if (x >= 0.001 && x <= 0.009) {
// something
}
你可以自己写一个between()函数:
You could write yourself a "between()" function:
function between(x, min, max) {
return x >= min && x <= max;
}
// ...
if (between(x, 0.001, 0.009)) {
// something
}
这篇关于检查if条件下的一系列数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!