I have string 'abcd.abcd..'
.
How to find how many .
symbols in the string?
string.match(/\\./)
string.match(/\./)
I tried the above and it doesn't work.
I can use string.split(".").length - 1
,
but how to do it with .match
method?
I have string 'abcd.abcd..'
.
How to find how many .
symbols in the string?
string.match(/\\./)
string.match(/\./)
I tried the above and it doesn't work.
I can use string.split(".").length - 1
,
but how to do it with .match
method?
Try the following:
var str = 'abcd.abcd..';
var symbolCount =(str.match(/\./g) || []).length;
console.log(symbolCount);
You're missing the global flag
var str = 'abc.d.';
try {
console.log(str.match(/\./g).length);
}
catch(error){
console.log(0);
}
You need the g
flag to match all occurrences. Then you can get the length of that match.
Here is how to find how many .
character in the string:
console.log(("abcd.abcd..".match(/\./g) || []).length); //logs 3