Ruth Ruth - 7 days ago 5x
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?


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.