mathiasbynens / emoji-regex

A regular expression to match all Emoji-only symbols as per the Unicode Standard.
https://mths.be/emoji-regex
MIT License
1.73k stars 174 forks source link

Option of flagless search #19

Closed roderickhsiao closed 7 years ago

roderickhsiao commented 7 years ago

Hi Mathias,

We are currently having a use case that will run through array to check if the item is emoji

const emojiRegex = require('emoji-regex');
const textList = ['😁', '😂', '😃' ,'😄', '😅', '😆'];
const isEmoji = char => emojiRegex().text(char);
textList.forEach(char => every(isEmoji(char));

The global search state needs to be reset so we will need to call emojiRegex every time instead of just create once like.

const emojiRegex = require('emoji-regex');
const REGEX = emojiRegex();
const textList = ['😁', '😂', '😃' ,'😄', '😅', '😆'];
const isEmoji = char => REGEX.text(char);
textList.forEach(char => every(isEmoji(char));

probably really minor but there do have some perf difference if the use case is just to match single string.

https://jsperf.com/create-regex/1

Would like to hear about your idea and suggestion.

Thanks and Cheers!

mathiasbynens commented 7 years ago

Have you tried resetting lastIndex before using the regex?

const isEmoji = (char) => {
  REGEX.lastIndex = 0;
  return REGEX.text(char);
};
roderickhsiao commented 7 years ago

good call ;) Didn't think about it.

Thanks greatly