flutter-emoji icon indicating copy to clipboard operation
flutter-emoji copied to clipboard

Emoji Regex

Open febg11 opened this issue 5 years ago • 7 comments

I was wondering if you could help me with blocking emojis from being typed into input boxes.

The emoji 🌬️ (and a few others) still get typed into input boxes that have been blacklisted with your regex.

   return BlacklistingTextInputFormatter(
        EmojiParser.REGEX_EMOJI
    );

They dont get typed but they get counted as input. E.g. if you have a hint in the text box (or character count) and type the emoji the hint will disappear and the character count will increase to 1. There is actually no input in the box, just empty space.

febg11 avatar Jan 26 '20 16:01 febg11

If i get the string after typing them in the code unit field has [65039, 65039, 65039, 65039, 65039, 65039] as a value. This seems to be this https://unicode-table.com/en/FE0F/

febg11 avatar Jan 26 '20 16:01 febg11

Could you try and use this regex it seems to fix the problem https://github.com/mathiasbynens/emoji-regex/blob/master/index.js

febg11 avatar Jan 26 '20 17:01 febg11

@febg11 it's kinda different, the regex by EmojiParser is to parse emoji, not to filter. If you want to filter emoji runes from TextField or similar widgets, you need to filter the \ufe0f character.

Use this

TextField(
  onChanged: (text) {
    print("Word count: " + text.length.toString());
  },
  inputFormatters: [
    BlacklistingTextInputFormatter(RegExp(
        r'(\u00a9|\u00ae|[\u2000-\u3300]|\ud83c[\ud000-\udfff]|\ud83d[\ud000-\udfff]|\ud83e[\ud000-\udfff]|\ufe0f)'
    ))
  ],
)

petehouston avatar Feb 10 '20 20:02 petehouston

Hi @petehouston

Sorry for not replying for a long time I only just got around to doing this. I have tested your regex which fixes a lot of my problems however it does not detect the English, Welsh and Scottish flags as well as the unofficial flags which can be seen on here.

This is part of my test that fails.

    String regexNew = r'(\u00a9|\u00ae|[\u2000-\u3300]|\ud83c[\ud000-\udfff]|\ud83d[\ud000-\udfff]|\ud83e[\ud000-\udfff]|\ufe0f)';
    RegExp regex = RegExp(regexNew);

    print("Starting british flags");
    String flags2 = "🏴󠁧󠁢󠁥󠁮󠁧󠁿🏴󠁧󠁢󠁳󠁣󠁴󠁿🏴󠁧󠁢󠁷󠁬󠁳󠁿";
    String flags2Output = flags2.replaceAll(regex, "");
    expect(flags2Output, "");
    print("Starting Unofficial Flags Emojis");
    String unofficialFlagsEmojis = "🏴󠁢󠁳󠁡󠁫󠁿🏴󠁢󠁳󠁢󠁹󠁿🏴󠁢󠁳󠁢󠁩󠁿🏴󠁢󠁳󠁢󠁰󠁿🏴󠁢󠁳󠁣󠁯󠁿🏴󠁢󠁳󠁣󠁳󠁿🏴󠁢󠁳󠁣󠁥󠁿🏴󠁢󠁳󠁣󠁫󠁿🏴󠁢󠁳󠁥󠁧󠁿🏴󠁢󠁳󠁥󠁸󠁿🏴󠁢󠁳󠁦󠁰󠁿🏴󠁢󠁳󠁧󠁣󠁿🏴󠁢󠁳󠁨󠁩󠁿🏴󠁢󠁳󠁨󠁴󠁿🏴󠁢󠁳󠁩󠁮󠁿🏴󠁢󠁳󠁬󠁩󠁿🏴󠁢󠁳󠁭󠁣󠁿🏴󠁢󠁳󠁭󠁧󠁿🏴󠁢󠁳󠁭󠁩󠁿🏴󠁢󠁳󠁮󠁯󠁿🏴󠁢󠁳󠁮󠁳󠁿🏴󠁢󠁳󠁮󠁥󠁿🏴󠁢󠁳󠁲󠁩󠁿🏴󠁢󠁳󠁲󠁣󠁿🏴󠁢󠁳󠁳󠁳󠁿🏴󠁢󠁳󠁳󠁯󠁿🏴󠁢󠁳󠁳󠁡󠁿🏴󠁢󠁳󠁳󠁥󠁿🏴󠁢󠁳󠁳󠁷󠁿🏴󠁢󠁳󠁷󠁧󠁿";
    String unofficialFlagsEmojisOutput = unofficialFlagsEmojis.replaceAll(regex, "");
    expect(unofficialFlagsEmojisOutput, "");

I am not sure what the unofficial flags even are.

Could you help me adjust the regex to ensure that the British flags and possibly the unofficial flags are black listed in the regex.

Thank you so much for all you have done.

febg11 avatar Apr 04 '20 12:04 febg11

Interesting. I will take a look on those emojis.

petehouston avatar Apr 10 '20 00:04 petehouston

Thanks Pete I really appreciate your help 😊

febg11 avatar Apr 17 '20 08:04 febg11

Hi Pete did you manage to take a look at this?

febg11 avatar May 28 '20 18:05 febg11