簡體   English   中英

如何知道正則表達式是否在使用量詞設置的字符大小限制的下限或上限上失敗

[英]How to know if a regex failed on the lower or upper bound of a character size limit set with a quantifier

量詞可用於正則表達式中以匹配大小限制內的字符串:

"54 343 2356 2".match(/\d{3,4}/)  // 343, 2356

如果我想針對帶有量詞的正則表達式測試字符串,我怎么知道測試是否在字符大小限制的下限或上限上失敗?

/\d{3,4}/.test("54525")

如果測試在字符大小限制的下限或上限上失敗?

為此,您不需要正則表達式,而是執行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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM