JavaScript: How many times a character occurs 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. The RegExp is described below:

[ // Start Character Group
^ // Not operator in character group
a // The character "a"
] // End character group

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)