Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我的问题是:我如何才能只计算字符串中的数字,例如,如果我有:(55)-555-34 以获得输出 7,我的意思是排除破折号和括号。干杯!
您可以将.match()与/\d/g 正则表达式一起使用:
/\d/g
"(55)-555-34".match(/\d/g).length //result=>7
用 删除所有非数字replace,并得到结果字符串的长度:
replace
str.replace(/\D/g,"").length
与应用程序相比,这具有match您不需要检查null结果(以防找不到匹配项)的优势。
match
null