Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
182
rated 0 times [  185] [ 3]  / answers: 1 / hits: 28812  / 13 Years ago, fri, july 15, 2011, 12:00:00

I have a string that devides some data using ','. Now I want to count the occurences of ',' in that string. I tried:



var match = string.match('/[,]/i');


But this gives me null If I try to get the length of the match array. Any ideas?


More From » javascript

 Answers
117

If you need to check the occurances of a simple pattern as , then better don't use regular expressions.



Try:



var matchesCount = string.split(,).length - 1;

[#91172] Wednesday, July 13, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
darleneh

Total Points: 231
Total Questions: 110
Total Answers: 94

Location: Spain
Member since Thu, Dec 23, 2021
2 Years ago
;