【问题标题】:Regex for detecting emojis with condition用于检测带有条件的表情符号的正则表达式
【发布时间】:2021-04-20 18:04:00
【问题描述】:

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

  • 除了表情之外没有其他任何东西
  • 表情符号不超过 5 个

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

const myRegex = "??"

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

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

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

【问题讨论】:

    标签: javascript regex emoji


    【解决方案1】:

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

    // 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);
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-06-26
      • 1970-01-01
      • 2023-03-24
      • 2018-11-28
      • 1970-01-01
      • 2019-05-10
      • 2019-08-15
      相关资源
      最近更新 更多