JQuery only counts numbers in a string

My question is: how can I only count numbers from a string, for example, if I have: (55) -555-34 to get output 7, I mean, for example, exclude dashes and brackets. Hooray!

+5
source share
2 answers

You can use . match () with /\d/gRegular expression:

"(55)-555-34".match(/\d/g).length
//result=>7
+16
source

Remove all non numbers with replaceand get the line length of the result:

str.replace(/\D/g,"").length

This has the advantage over the application matchthat you do not need to check the results null(if there is no match found).

+2
source

All Articles