Regex to find emoji names with colon and skintone

794 Views Asked by At

I'm using EmojiMart for my parser.

I've seen this related question but it seem to be different from mine.

So I need to return the emoji names or :code: for them to be able to decode it.

So example I have this text:

:+1::skin-tone-6::man-pouting:Hello world:skin-tone- 
6:lalalalla:person_with_pouting_face: :poop::skin-tone-11: mamamia 
:smile: :skin-tone-6:

It should match the whole :+1::skin-tone-6: and not a separate :+1:, :skin-tone-6:: - only if there’s no space between them. (notice the space between :smile: and :skin-tone-6: )

Conditions:

It should only match the :code::skintone: if skintone is 2-6

If I do str.split(regex) this is my expected result (array):

- :+1::skin-tone-6:
- :man-pouting:
- Hello world
- :skin-tone-6:
- lalalalla
- :person_with_pouting_face: 
- :poop:
- :skin-tone-11: 
-  mamamia 
- :smile: 
- :skin-tone-6:
1

There are 1 best solutions below

9
On BEST ANSWER

You may use String#split() with the

/(:[^\s:]+(?:::skin-tone-[2-6])?:)/

regex. See the regex demo.

Details

  • : - a colon
  • [^\s:]+ - 1+ chars other than whitespace and :
  • (?:::skin-tone-[2-6])? - an optional sequence of
    • ::skin-tone- - a literal substring
    • [2-6] - a digit from 2 to 6
  • : - a colon.

JS demo:

var s = ":+1::skin-tone-6::man-pouting:Hello world:skin-tone-6:lalalalla:person_with_pouting_face: :poop::skin-tone-11: mamamia :smile: :skin-tone-6:";
var reg = /(:[^\s:]+(?:::skin-tone-[2-6])?:)/;
console.log(s.split(reg).filter(x => x.trim().length !=0 ));

The .filter(x => x.trim().length !=0 ) removes all blank items from the resulting array. For ES5 and older, use .filter(function(x) { return x.trim().length != 0; }).