2

Is there any examples of regex for entering text(letters) without emojis ?

I tried to inverse regular expression but it works not correctly

/(?!:\ud83c[\udf00-\udfff]|\ud83d[\udc00-\ude4f]|\ud83d[\ude80-\udeff])$/g

In this case if I'm entering letters(a-z) the string is not valid

Vlad
  • 94
  • 1
  • 1
  • 7
  • Check [Prevent emoji and zero width characters in text input](https://stackoverflow.com/questions/40628196/prevent-emoji-and-zero-width-characters-in-text-input) (remove `|\s` from that pattern). – Wiktor Stribiżew Jun 19 '18 at 13:15
  • Also, see [How to remove emoji code using javascript?](https://stackoverflow.com/questions/10992921/how-to-remove-emoji-code-using-javascript) – Wiktor Stribiżew Jun 19 '18 at 13:17
  • @WiktorStribiżew thank you for your help, but I would like to validate form so I need regex to put it in pattern of html input element – Vlad Jun 19 '18 at 13:52
  • Try https://regex101.com/r/Lrgh4V/1 – Wiktor Stribiżew Jun 19 '18 at 16:04

1 Answers1

9

Your RegExp character set is incomplete as it doesn't include every Emoji. I got the full unicode character set of all 845 Emojis in existence from {another answer on Stack Overflow}

The inverse RegExp is unnecessary for this case, and yours didn't have the right syntax. Proper form: ^((?!••••••••••).)*$ as mentioned in {another answer on Stack Overflow}

Here's the new RegExp:

^[^\u{203C}\u{2049}\u{20E3}\u{2122}\u{2139}\u{2194}-\u{2199}\u{21A9}-\u{21AA}\u{231A}-\u{231B}\u{23E9}-\u{23EC}\u{23F0}\u{23F3}\u{24C2}\u{25AA}-\u{25AB}\u{25B6}\u{25C0}\u{25FB}-\u{25FE}\u{2600}-\u{2601}\u{260E}\u{2611}\u{2614}-\u{2615}\u{261D}\u{263A}\u{2648}-\u{2653}\u{2660}\u{2663}\u{2665}-\u{2666}\u{2668}\u{267B}\u{267F}\u{2693}\u{26A0}-\u{26A1}\u{26AA}-\u{26AB}\u{26BD}-\u{26BE}\u{26C4}-\u{26C5}\u{26CE}\u{26D4}\u{26EA}\u{26F2}-\u{26F3}\u{26F5}\u{26FA}\u{26FD}\u{2702}\u{2705}\u{2708}-\u{270C}\u{270F}\u{2712}\u{2714}\u{2716}\u{2728}\u{2733}-\u{2734}\u{2744}\u{2747}\u{274C}\u{274E}\u{2753}-\u{2755}\u{2757}\u{2764}\u{2795}-\u{2797}\u{27A1}\u{27B0}\u{2934}-\u{2935}\u{2B05}-\u{2B07}\u{2B1B}-\u{2B1C}\u{2B50}\u{2B55}\u{3030}\u{303D}\u{3297}\u{3299}\u{1F004}\u{1F0CF}\u{1F170}-\u{1F171}\u{1F17E}-\u{1F17F}\u{1F18E}\u{1F191}-\u{1F19A}\u{1F1E7}-\u{1F1EC}\u{1F1EE}-\u{1F1F0}\u{1F1F3}\u{1F1F5}\u{1F1F7}-\u{1F1FA}\u{1F201}-\u{1F202}\u{1F21A}\u{1F22F}\u{1F232}-\u{1F23A}\u{1F250}-\u{1F251}\u{1F300}-\u{1F320}\u{1F330}-\u{1F335}\u{1F337}-\u{1F37C}\u{1F380}-\u{1F393}\u{1F3A0}-\u{1F3C4}\u{1F3C6}-\u{1F3CA}\u{1F3E0}-\u{1F3F0}\u{1F400}-\u{1F43E}\u{1F440}\u{1F442}-\u{1F4F7}\u{1F4F9}-\u{1F4FC}\u{1F500}-\u{1F507}\u{1F509}-\u{1F53D}\u{1F550}-\u{1F567}\u{1F5FB}-\u{1F640}\u{1F645}-\u{1F64F}\u{1F680}-\u{1F68A}]*$
  • ^ asserts position at the start of the string.

  • [^••••••••••] is a negated character class. It matches every character that is NOT in the list, i.e. not an Emoji.

  • * is a quantifier that matches any number of these non-Emoji characters.

  • $ asserts position at the end of the string.

A working example of the RegExp inserted into <input pattern="">

<html><head>
    <meta charset="UTF-8">
</head><body>
    <form method="GET" action="javascript:console.log('VALID INPUT');">
        <input type="text" pattern="^[^\u{203C}\u{2049}\u{20E3}\u{2122}\u{2139}\u{2194}-\u{2199}\u{21A9}-\u{21AA}\u{231A}-\u{231B}\u{23E9}-\u{23EC}\u{23F0}\u{23F3}\u{24C2}\u{25AA}-\u{25AB}\u{25B6}\u{25C0}\u{25FB}-\u{25FE}\u{2600}-\u{2601}\u{260E}\u{2611}\u{2614}-\u{2615}\u{261D}\u{263A}\u{2648}-\u{2653}\u{2660}\u{2663}\u{2665}-\u{2666}\u{2668}\u{267B}\u{267F}\u{2693}\u{26A0}-\u{26A1}\u{26AA}-\u{26AB}\u{26BD}-\u{26BE}\u{26C4}-\u{26C5}\u{26CE}\u{26D4}\u{26EA}\u{26F2}-\u{26F3}\u{26F5}\u{26FA}\u{26FD}\u{2702}\u{2705}\u{2708}-\u{270C}\u{270F}\u{2712}\u{2714}\u{2716}\u{2728}\u{2733}-\u{2734}\u{2744}\u{2747}\u{274C}\u{274E}\u{2753}-\u{2755}\u{2757}\u{2764}\u{2795}-\u{2797}\u{27A1}\u{27B0}\u{2934}-\u{2935}\u{2B05}-\u{2B07}\u{2B1B}-\u{2B1C}\u{2B50}\u{2B55}\u{3030}\u{303D}\u{3297}\u{3299}\u{1F004}\u{1F0CF}\u{1F170}-\u{1F171}\u{1F17E}-\u{1F17F}\u{1F18E}\u{1F191}-\u{1F19A}\u{1F1E7}-\u{1F1EC}\u{1F1EE}-\u{1F1F0}\u{1F1F3}\u{1F1F5}\u{1F1F7}-\u{1F1FA}\u{1F201}-\u{1F202}\u{1F21A}\u{1F22F}\u{1F232}-\u{1F23A}\u{1F250}-\u{1F251}\u{1F300}-\u{1F320}\u{1F330}-\u{1F335}\u{1F337}-\u{1F37C}\u{1F380}-\u{1F393}\u{1F3A0}-\u{1F3C4}\u{1F3C6}-\u{1F3CA}\u{1F3E0}-\u{1F3F0}\u{1F400}-\u{1F43E}\u{1F440}\u{1F442}-\u{1F4F7}\u{1F4F9}-\u{1F4FC}\u{1F500}-\u{1F507}\u{1F509}-\u{1F53D}\u{1F550}-\u{1F567}\u{1F5FB}-\u{1F640}\u{1F645}-\u{1F64F}\u{1F680}-\u{1F68A}]*$">
        <input type="submit" value="Submit">
    </form>
<body></html>

The pattern attribute doesn't block Emojis from being entered, but it prevents form submission.

clickbait
  • 2,818
  • 1
  • 25
  • 61
  • 1
    Note that the `\u{203C}` notation will only work in ES6 compatible JS environment, it won't work in IE and Safari (as of now). – Wiktor Stribiżew Jun 19 '18 at 18:56
  • 1
    @WiktorStribiżew thanks a lot for your help. Is there any other variants for regexp to be compatible with ES5? For now I'm getting `Invalid regular expression` when trying to save regex in variable or new RegExp(...) – Vlad Jun 20 '18 at 09:13
  • 2
    @Vlad Use https://regex101.com/r/Lrgh4V/1. It will deal with all empojis in 11.0 emoji Unicode standard. It is long, but it does the job correctly. – Wiktor Stribiżew Jun 20 '18 at 09:14
  • 1
    @WiktorStribiżew Is it possible to have regex to match symbols slash (\u) and anything else after it ? i.e /^(?!/u{4-5})/ In other words I'd have a common pattern – Vlad Jun 20 '18 at 11:42
  • 2
    This regEx is giving me `Range out of order in character class` – zamil Jun 06 '19 at 11:43