Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery count only numbers in a string

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

like image 411
Moozy Avatar asked Jul 26 '12 07:07

Moozy


People also ask

How to count number of digits in string?

isDigit(s. charAt(i)) will count the number of digits in the given string. 'Character. isDigit' will count the number of each digit in the string.

How to count numbers in string js?

To count the number of digits in a string, use the replace() method to replace all non digit characters with an empty string and access the length property on the result. The replace method returns a new string with the matches replaced.

How do I count the number of characters in a string using jQuery?

Answer: Use the jQuery . length property The jQuery . length property can be used to get or find out the number of characters in a string. You can also use this property to calculate the number of elements in a jQuery object.


1 Answers

You could use .match() with /\d/g Regular Expression:

"(55)-555-34".match(/\d/g).length
//result=>7
like image 145
Engineer Avatar answered Oct 19 '22 23:10

Engineer