用于检测带条件的表情符号的正则表达式

Regex for detecting emojis with condition

假设我们有一个字符串变量,其中可能包含表情符号。我正在尝试找到一种方法来选择那些字符串:

This repo 在检测各种表情符号方面效果很好,但我想知道如何在其正则表达式上应用我的规则。

const myRegex = "??"

const mString1 = ""
myRegex.test(mString1) // true

const mString2 = "Text‍"
myRegex.test(mString2) // false

const mString3 = ""
myRegex.test(mString3) // false

所以,基本上你想要的是以下内容:

// detects if string consists of 0 to 5 emojis  
const regex = /^(emoji){0,5}$/;

现在唯一缺少的部分是该正则表达式中的实际表情符号检测。我们可以从您引用的 emoji-regex 库中提取它:

const emojiRegex = require('emoji-regex/RGI_Emoji.js');

const regex = new RegExp("^(" + emojiRegex().source + "){0,5}$", emojiRegex().flags);

没有测试,但像这样的东西应该可以。