I'm trying to get the length of a javascript string in user-visible graphemes, ie ignoring combining characters (and surrogate pairs?). Is this possible, and if so, how would I go about it?

We're using the dojo toolkit on our project, but any general javascript solution would be great.

1 upvote
Answers to this question: //allinonescript.com/q/3744721/1352254 include the useful info that javascript uses UCS-2 instead of UTF-16, and indicate that this won't be possible. – Angus
It will be possible, it just won't be easy because you'll have to deal with some low-level Unicode issues. – hippietrail

3 Answers 11

up vote 1 down vote accepted

For the combining characters, look at the Derived Combining Class that lists all combining characters (among others). Since you're just interested in counting, you could just nuke them out -- leaves you with a slightly closer estimation.

In the post linked to by Angus, JavaScript strings outside of the BMP shows code to deal with surrogates. But the code actually does the contrary of what you want -- it splits the 0x10000+ codepoints into two codepoints. As far as JS is concerned it's one codepoint -- albeit a truncated one. Who cares? You're counting them, not displaying...

BUT, there's another category of codepoints you might want to deal with too, the non-printable characters. Anything under 0x20 of course, but there's plenty of others -- look at the 0x2000 range for instance. These are not visible either and should not be included in your count.

Thanks for the info, I didn't notice at the time that the linked question had example code, I had looked over it and thought JS just couldn't handle the low-level string stuff that would be necessary. – Angus

This open-source CoffeeScript implementation seems to work decently enough: https://github.com/devongovett/grapheme-breaker (if only it wasn't CS 😜)

Here is a pure JavaScript library that does just that:


It implements the Unicode UAX-29 standard in all its edge cases that you're likely to miss in a home-brew solution, like non-Latin diacritics, Hangul (Korean) jamo characters, emoji, multiple combining marks, etc.

Not the answer you're looking for? Browse other questions tagged or ask your own question.