[英]How to know if a regex failed on the lower or upper bound of a character size limit set with a quantifier
Quantifiers can be used in regular expressions to match strings within a size limit:量词可用于正则表达式中以匹配大小限制内的字符串:
"54 343 2356 2".match(/\d{3,4}/) // 343, 2356
If I wanted to test a string against a regex with a quantifier, how would I know if the test failed on the lower or upper bound of the character size limit?如果我想针对带有量词的正则表达式测试字符串,我怎么知道测试是否在字符大小限制的下限或上限上失败?
/\d{3,4}/.test("54525")
if the test failed on the lower or upper bound of the character size limit?
如果测试在字符大小限制的下限或上限上失败?
For that you don't need regex, Instead do an if-else conditioning , So count number of digit in that number:为此,您不需要正则表达式,而是执行if-else 条件,因此计算该数字中的位数:
let x = "54525"; if (x.length < 3) { console.log(x + ' has less digits than 3'); } else if (x.length > 4) { console.log(x + ' has more digits than 4'); } else { console.log(x + ' has exactly number of digits between 3 or 4'); }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.