how can I count the number of integers in a string using jQuery or javascript?
For example g66ghy7 = 3
how can I count the number of integers in a string using jQuery or javascript?
For example g66ghy7 = 3
alert("g66ghy7".replace(/[^0-9]/g,"").length);
Look here.
I find this to look pretty/simple:
var count = ('1a2b3c'.match(/\d/g) || []).length
A RegExp will probably perform better (it appears):
var r = new RegExp('\\d', 'g')
, count = 0
while(r.exec('1a2b3c')) count++;
The simplest solution would be to use a regular expression to replace all but the numeric values and pull out the length afterwards. Consider the following:
var s = 'g66ghy7';
alert(s.replace(/\D/g, '').length); //3
A simple for can solve this:
const value = "test:23:string236";
let totalNumbers = 0;
for (let i = 0; i < value.length; i++) {
const element = value[i];
if (isFinite(element)) {
totalNumbers++;
}
}
console.log(totalNumbers);