Search code examples
javascriptunicodegrapheme

Get grapheme character count in javascript strings?


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.


Solution

  • 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.