Ruth Ruth - 11 months ago 57
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.