Javascript:如何检查字符串中是否存在两个数字?(正则表达式)

Javascript: How to check if two numbers exist in string? (regex)

本文关键字:正则表达式 存在 两个 数字 是否 字符串 何检查 Javascript 检查      更新时间:2023-09-26

我有这个字符串:

testString = "child 4 to 10 years old";

如何检查字符串是否包含两个(或多个(数字?

testString.match("??")

谢谢!

如果字符串至少包含两个数字,则会找到匹配项。

testString.match(/(?:.*?'b'd+'b){2}/)

/(?:.*?'b'd+'b){2}/.test(str);

如果你也想处理十进制数字,那么试试这个,

/(?:.*?'b'd+(?:'.'d+)?'b){2}/.test(str);
   if (testString.match(/('d+)/).length >= 2) {

是一个非常简单/可读的解决方案。

"String0With1Some2Words3And4Integers0000".split('').reduce((val, acc) => (
    val  + (parseInt(acc) ? 1 : 0))
, 0)