| Viewed 182 times

How to count the Letters and Digits in javaScript?

I need to create a function that takes a string and calculates the number of letters and digits within it

1 Answer
Mohammad Qandeel

Its can be done using the match method and length 

const countAll = str => ({
  LETTERS: (str.match(/[a-z]/gi) || []).length,
  DIGITS: (str.match(/\d/g) || []).length


countAll('asd555') // {LETTERS: 3, DIGITS: 3}

Related Questions