Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 561
Angus Avatar asked Apr 23 '12 20:04

Angus


1 Answers

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.

like image 179
Orlin Georgiev Avatar answered Oct 07 '22 00:10

Orlin Georgiev