Get grapheme character count in javascript strings

2019-05-09 13:49发布

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.

3条回答
仙女界的扛把子
2楼-- · 2019-05-09 14:20

Here is a pure JavaScript library that does just that:

https://github.com/orling/grapheme-splitter

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.

查看更多
三岁会撩人
3楼-- · 2019-05-09 14:20

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

查看更多
来,给爷笑一个
4楼-- · 2019-05-09 14:26

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.

查看更多
登录 后发表回答