简体   繁体   English

如何知道正则表达式是否在使用量词设置的字符大小限制的下限或上限上失败

[英]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.

相关问题 我如何通过按键事件代码知道该字符是否刚刚输入 <textarea> 是小写还是大写? - How do I know, via key event code, if the character just entered in a <textarea> is lower or upper case? 使用正则表达式设置字符数限制 - Set character limit with regex 我如何知道在 JavaScript 中构造 Set 的大小限制是多少? - How do I know what is the size limit for constructing a Set in JavaScript? 如何测试字符是数字,大写还是小写? - How to test whether a character is numeric, upper case, or lower case? 正则表达式只允许 az 中的任何字符,包括小写和大写、任意数字和特殊字符。 -_ 只要 - Regex to allow only any character from a-z both lower and upper, any number and special characters . -_ only Javascript查找上限和下限之间的值 - Javascript lookup for value between upper and lower limit 在给定上限和下限的情况下,如何自动为 CSS 属性插入 n 个中间值? - How can I automatically interpolate n intermediate values for a CSS property given an upper and lower bound? 如何在 Highcharts 中为轴设置最小上限? - How do I set a minimum upper bound for an axis in Highcharts? 如何为谷歌图表设置 x 轴的下限? - How can I set the lower bound on the x axis for a google chart? 如何使用大写/小写 - How to use Upper/Lower case
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM