Ruth Ruth - 1 year ago 103
Javascript Question

Javascript: How many times a character occurs in a string

Is there a simple way to check how many times a character appears in a String?

Answer Source

You could remove any other character in the string and check the length:

str.replace(/[^a]/g, "").length

Here it is counted how many as are in str.