Split string of emoji characters by split function with regex

Viewed 1951

I want to use split function of Javascript to split string of emoji characters. In stackoverflow there are many question like that, but I cannot find any completed solutions. So I do it by my own way:

a) Use split function with regex.

b) Split emoji characters by regex unicode matches: from \uD800 to \uDBFF and from \uDC00 to \uDFFF.

c) In this regex, exclude zero-with-joiner (\u200D) and variation selector (\uFE0F) characters. So I wrote as follows:

var p = '‍‍‍‍❤️‍‍';

and split it:

var split = p.split(/(?![\u200D\uFE0F])([\uD800-\uDBFF][\uDC00-\uDFFF])/);

But the result is wrong :(

["", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "‍", "", "‍", "", "‍", "", "", "", "", "", "", "", "‍❤️‍", "", "‍", "", ""]

Did I use the excluding selector for regex right? If right, the error caused by my idea? The expected result need to be: ["", "", "", "", "", "‍‍‍", "", "", "‍❤️‍‍"]

===

I want to update info. I solved this problem for my site: https://www.emojionline.org. You can test. I just use a dictionary that hold all emojis and I use the replace function to replace every emoji by |emoji|. And I can split string emoji by symbol |. That works well :)

1 Answers
Related