I'm trying multiple regex expressions but I cannot get them to work.

I have a simple input where the users can type whatever they like, only that the final result must contain emojis. To achieve this, I have to remove every character from the string which is not an emoji, and then check if the length is >= 1.

So basically this: asf..?23kj😔gasdf..😅,fwe34 should become this: 😔😅. Then I'd check the length to confirm that it's >=1 and I'd be good to go.

I'm usign Mac OS very own virtual emoji keyboard to write the emojis along the text in the input, in case you find it useful to know.

share|improve this question
    
Add the code you've already attempted to your question. – Andy Aug 10 '15 at 11:28
    
"I'm trying multiple regex expressions" - and have posted none? what does an emoji look like in a javascript string? – Jaromanda X Aug 10 '15 at 11:29
    
It shows like just an icon, although it has attached a unicode internally (apps.timwhitlock.info/emoji/tables/unicode). – tontonton Aug 10 '15 at 11:30
    
I'm just using the "replace" function to remove the chars, something like tags.replace(/^[\u1f600-\u1f64f]/g, 'X');, which instead of removing the characters should change them for an 'X'. – tontonton Aug 10 '15 at 11:32
    
What about .replace(/\ud83d[\ude00-\ude4f]/g, '') to remove all emojis, and then count s.length - s_new.length? – Wiktor Stribiżew Aug 10 '15 at 11:37
up vote 2 down vote accepted

From what I get from comments, some of this may or may not help -



To validate a string contains 1 or more emoji :

 # ^(?=[\S\s]*(?:\ud83d[\ude00-\ude4f]))

 ^ 
 (?=
      [\S\s]* 
      (?: \ud83d [\ude00-\ude4f] )
 )


To remove only emoji, leaving the rest (global) :

Find: (?:\ud83d[\ude00-\ude4f])*((?:(?!\ud83d[\ude00-\ude4f])[\S\s])+)(?:\ud83d[\ude00-\ude4f])*
Replace: $1

 (?: \ud83d [\ude00-\ude4f] )*
 (                                       # (1 start)
      (?:
           (?! \ud83d [\ude00-\ude4f] )
           [\S\s] 
      )+
 )                                       # (1 end)
 (?: \ud83d [\ude00-\ude4f] )*


To remove everything but emoji (global) :

Find: ((?:\ud83d[\ude00-\ude4f])*)(?:(?!\ud83d[\ude00-\ude4f])[\S\s])+((?:\ud83d[\ude00-\ude4f])*)
Replace: $1$2

 (                                       # (1 start)
      (?: \ud83d [\ude00-\ude4f] )*
 )                                       # (1 end)
 (?:
      (?! \ud83d [\ude00-\ude4f] )
      [\S\s] 
 )+
 (                                       # (2 start)
      (?: \ud83d [\ude00-\ude4f] )*
 )                                       # (2 end)

edit: To use different emoji utf16 ranges do this

Different high surrogates:

(?:
     High_surrogate_A [Low_surrogate_start_A-Low_surrogate_end_A]
  |  High_surrogate_B [Low_surrogate_start_B-Low_surrogate_end_B]
  |  High_surrogate_C [Low_surrogate_start_C-Low_surrogate_end_C]
)

or, same high surrogate, different low surrogate ranges:

(?:
     High_surrogate [Low_surrogate_start1-Low_surrogate_end1Low_surrogate_start2-Low_surrogate_end2]
)

or, mix:

(?:
     High_surrogate_A [Low_surrogate_startA1-Low_surrogate_endA1Low_surrogate_startA2-Low_surrogate_endA2]
  |  High_surrogate_B [Low_surrogate_start_B-Low_surrogate_end_B]
)

Where you see:

(?: \ud83d [\ude00-\ude4f] )*

substitue one of the above in placeholder HERE

HERE*

Where you see:

(?! \ud83d [\ude00-\ude4f] )

substitue one of the above in placeholder HERE

(?! HERE )


Note- you can add a High-surrogate range as well, however all the high surrogates must share the same low-surrogate(s) range.

share|improve this answer
    
This works almost fine, but it's mostly due to new emojis being added in other UTF ranges, so they are removed when they shouldn't be. This code works basically fine, but as I say it removes more than it should. How can I add more ranges so the new emojis doesn't get removed? And where did you find those ranges, by the way, if you can tell me. – tontonton Aug 13 '15 at 7:28

Emojis are in a supplemental plane, at 1F601 - 1F64F. Normal JS regexps do not work (well) with such code points. If you have available an ES6 implementation which supports the new u flag (Babel does), that should do the job for you.

tags.replace(/^[\u{1f600}-\u{1f64f}]/g, 'X');

Otherwise, you need to consider using a library such as https://github.com/mathiasbynens/regenerate.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.