0

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?

Noam Hacker
  • 4,671
  • 7
  • 34
  • 55
  • 1
    What you provided here seems to work fine – https://jsfiddle.net/06o1vnu9/. I'd check the file encodings for your scripts, ensure they match. Otherwise, the character code used to save `✔` may not be the same between them. – Jonathan Lonowski Dec 26 '15 at 05:21
  • @JonathanLonowski thanks for putting that together. you're probably right about the encodings, but luckily I found a safer solution (answered my own question) – Noam Hacker Dec 26 '15 at 05:25

2 Answers2

2

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.

Noam Hacker
  • 4,671
  • 7
  • 34
  • 55
1

There is no problem with doing a string match with unicode. The match method returns an array of matched elements. In order to count the total you then need the total number of elements in that array. I believe the code you are looking for is:

myString.match(/✔/g).length
Jerome Hudak
  • 116
  • 1
  • 4
  • 1
    Note: `.match()` will return `null` if no matches are found. The `|| []` is to ensure an array is always given since you can't read the `.length` of `null`. – Jonathan Lonowski Dec 26 '15 at 05:24