[removed] How many times a character occurs in a string?

前端 未结 6 2104
傲寒
傲寒 2020-12-13 03:15

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

6条回答
  •  长情又很酷
    2020-12-13 03:53

    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.

提交回复
热议问题