1

Is regex possible to calculate and to count a specific character in a string?

For instance the string value "asdfasdfasdf" and you want to count how many "a" in the value?

Tried to find a solution but I cant find one.

KLN
  • 413
  • 3
  • 8

1 Answers1

5

You can use match()

 var count = "asdfasdfasdf".match(/a/g).length;
 alert(count);
Pranav C Balan
  • 113,687
  • 23
  • 165
  • 188