Noam Hacker Noam Hacker - 14 days ago 5
Javascript Question

Javascript/dingbats: counting the number of ✔'s in a string

I am trying to count the checkmarks in a given string. my string may be:

var myString = "one ✔ two ✔ three ✔"


I have tried using
myString.match(/✔/g) || []).length;
, but this is returning
0
(I believe that happens because ✔ is a dingbat symbol).

I know the unicode for "✔" is 2714, can I use this in my expression?

Answer

I have solved my problem: I used the "unicode escape sequence" \u with the 2714 unicode:

.match(/\u2714/g) || []).length;

The problem was likely due to a character encoding issue (see the comments on my question), but this solution seems to be a safe choice.

Comments