0

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?

Terry Young
  • 3,531
  • 1
  • 20
  • 21

3 Answers3

1

Try the following:

var str = 'abcd.abcd..';
var symbolCount =(str.match(/\./g) || []).length;
console.log(symbolCount);
Mamun
  • 66,969
  • 9
  • 47
  • 59
0

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.

chevybow
  • 9,959
  • 6
  • 24
  • 39
  • Will throw an error if nothing is matched... – Marcos Casagrande May 29 '18 at 22:49
  • @MarcosCasagrande OP provided a specific string which will not result in error. If they want to do error handling they can refactor their program to do so. On SO we answer questions we don't write full programs for people (which includes error handling if they try to use this example for a string that can result in error). I don't understand the mass downvotes for not writing a complete program for what in reality is a simple question. – chevybow May 29 '18 at 22:53
  • I know how SO works, and providing an answer that is not good, deserves a -1. If the OP has a hardcoded string, he wouldn't need any `match`, he would just hardcode `3`. So you have to assume, `string` doesn't always contains a dot. So your answer is wrong. – Marcos Casagrande May 29 '18 at 22:55
  • You can also assume that `string` isn't a string if the data is coming from somewhere else- how far do you want to make your assumptions to extend to scope past the point of the question? – chevybow May 29 '18 at 22:57
  • He says he wants to count the occurrences in a **string**, so that far. – Marcos Casagrande May 29 '18 at 22:57
  • That could just be the name of the string he used in example @MarcosCasagrande – chevybow May 29 '18 at 22:58
  • Why would someone need to count the occurrences of a string in a hardcoded string, he already knows the answer, that string will likely change, and that string can contain zero ocurrences, just accept the fact that your answer is wrong and you shuould change it, otherwise the -1 stands. – Marcos Casagrande May 29 '18 at 22:59
  • I edit answer. Please let me know if its up to your standards @MarcosCasagrande – chevybow May 29 '18 at 23:04
  • Not my standards, Your answers will be seen by people who probably knows less than you, and by providing a half baked answer you're not doing them any good nor the site. It's not really up to my standards, but will work in every case. – Marcos Casagrande May 29 '18 at 23:06
0

Here is how to find how many . character in the string:

console.log(("abcd.abcd..".match(/\./g) || []).length); //logs 3
Audwin Oyong
  • 2,247
  • 3
  • 15
  • 32
Eby Jacob
  • 1,418
  • 1
  • 10
  • 28
  • how can this snippet throw an error, the string has 3 matches – Eby Jacob May 29 '18 at 22:51
  • with that specific string, what if the string is: `'nodot'`, good luck trying to access `.length` of `null`. – Marcos Casagrande May 29 '18 at 22:51
  • what specific string? check the snippet, it has a string which has three dots in It, the user has to do all validation before doing any of these, what if the value is null, which code will work. All these needs data validation and prechecks, which is understood. Not sure why would you say the specific answer here can have no matches. – Eby Jacob May 29 '18 at 22:54
  • He wants to know how to count the occurrences of dots in a string, so there is a possibility that there are no dots whatsoever, and in those cases your answer will fail, hence the -1. – Marcos Casagrande May 29 '18 at 22:57