有没有办法检查 JS 中的字符串是不是是一个表情符号?

Posted

技术标签:

【中文标题】有没有办法检查 JS 中的字符串是不是是一个表情符号?【英文标题】:Is there a way to check if a string in JS is one single emoji?有没有办法检查 JS 中的字符串是否是一个表情符号? 【发布时间】:2020-03-15 10:48:37 【问题描述】:

问题很简单:我有一个字符串str,我如何检查str 是否是一个表情符号,而不是别的?此外,我不希望使用其他库。

匹配"????""⛹????‍♂️""3️⃣",但不匹配"????a""????""????????"

我无法找到解决方案,但到目前为止我已经尝试过以下一些方法:


尝试的解决方案 1 - 玩转长度和 ... 运算符

我了解到 emojis 占用超过 1 个字节,有些甚至占用 4 个字节,甚至更多...我们可以通过字符串的 length 属性来衡量:

console.log("????".length); // 2
console.log("????️".length); // 3
console.log("⛹????‍♂️".length); // 6

然后我发现... 运算符将这一点考虑在内并正确分隔数组中的表情符号 - 然后我可以看到结果数组的length 属性并检测它们是否不同。

str = "⛹????‍♂️";
if (str.length !== [...str].length) 
  // is emoji?
 else 
  // is not emoji

但这不会检查其他多字节字符,例如长度为 2 的 ????。此外,一些表情符号仍然会以奇怪的方式分开。


尝试的解决方案 2 - 正则表达式、正则表达式

当然,正则表达式值得研究,但我还没有找到可行的解决方案。

This answer 的正则表达式\u00a9|\u00ae|[\u2000-\u3300]|\ud83c[\ud000-\udfff]|\ud83d[\ud000-\udfff]|\ud83e[\ud000-\udfff] 可以很好地检测字符串是否有任何表情符号,但应用于我的情况会产生很多问题。这是我的测试:

A 部分 - 没有字符串正则表达式的开始/结束(^$

2A.1 str.match(regex) 非常不一致,它分解了一些表情符号和其他一些无法使用的符号。我没有办法确定它是否包含非表情符号字符,或者它是否包含多个表情符号:
let regex = /(\u00a9|\u00ae|[\u2000-\u3300]|\ud83c[\ud000-\udfff]|\ud83d[\ud000-\udfff]|\ud83e[\ud000-\udfff])/;

console.log("5️⃣".match(regex)); // [ '⃣', '⃣', index: 2, input: '5️⃣' ]
console.log("????".match(regex)); // [ '????', '????', index: 0, input: '????' ]
console.log("????️????️".match(regex)); // [ '????', '????', index: 0, input: '????️????️' ]
console.log("a⛅".match(regex)); // [ '⛅', '⛅', index: 1, input: 'a⛅' ]
2A.2 只要字符串中包含表情符号,regex.test(str) 就会返回 true,这不是我要寻找的行为:
let regex = /(\u00a9|\u00ae|[\u2000-\u3300]|\ud83c[\ud000-\udfff]|\ud83d[\ud000-\udfff]|\ud83e[\ud000-\udfff])/;

console.log(regex.test("5️⃣")); // true - correct
console.log(regex.test("a")); // false - correct
console.log(regex.test("????️????️")); // true - should be false
console.log(regex.test("hello ⛅!")); // true - should be false

B 部分 - 字符串正则表达式的开始/结束(^$

2B.1 str.match(regex) 出于某种原因在某些表情符号上返回 null。我不知道为什么,但我假设它与为什么 str.match(regex) 会在 A 部分中分解这些表情符号有某种关系:
let regex = /^(\u00a9|\u00ae|[\u2000-\u3300]|\ud83c[\ud000-\udfff]|\ud83d[\ud000-\udfff]|\ud83e[\ud000-\udfff])$/;

console.log("5️⃣".match(regex)); // null
console.log("????".match(regex)); // [ '????', '????', index: 0, input: '????' ]
console.log("????️".match(regex)); // null
console.log("⛅".match(regex)); // [ '⛅', '⛅', index: 1, input: 'a⛅' ]
console.log("????????".match(regex)); // null
2B.2 regex.test(str) 将在相同的表情符号上返回 false,而在 str.match(regex) 上返回 null
let regex = /^(\u00a9|\u00ae|[\u2000-\u3300]|\ud83c[\ud000-\udfff]|\ud83d[\ud000-\udfff]|\ud83e[\ud000-\udfff])$/;

console.log(regex.test("5️⃣")); // false - should be true
console.log(regex.test("????")); // true - correct
console.log(regex.test("????️")); // false - should be true
console.log(regex.test("⛅")); // true - correct
console.log(regex.test("????????")); // false - correct

C 部分 - 其他正则表达式

我找到了this one,但它给出了类似的不一致,虽然不一样/(?:[\u2700-\u27bf]|(?:\ud83c[\udde6-\uddff])2|[\ud800-\udbff][\udc00-\udfff]|[\u0023-\u0039]\ufe0f?\u20e3|\u3299|\u3297|\u303d|\u3030|\u24c2|\ud83c[\udd70-\udd71]|\ud83c[\udd7e-\udd7f]|\ud83c\udd8e|\ud83c[\udd91-\udd9a]|\ud83c[\udde6-\uddff]|[\ud83c[\ude01\uddff]|\ud83c[\ude01-\ude02]|\ud83c\ude1a|\ud83c\ude2f|[\ud83c[\ude32\ude02]|\ud83c\ude1a|\ud83c\ude2f|\ud83c[\ude32-\ude3a]|[\ud83c[\ude50\ude3a]|\ud83c[\ude50-\ude51]|\u203c|\u2049|[\u25aa-\u25ab]|\u25b6|\u25c0|[\u25fb-\u25fe]|\u00a9|\u00ae|\u2122|\u2139|\ud83c\udc04|[\u2600-\u26FF]|\u2b05|\u2b06|\u2b07|\u2b1b|\u2b1c|\u2b50|\u2b55|\u231a|\u231b|\u2328|\u23cf|[\u23e9-\u23f3]|[\u23f8-\u23fa]|\ud83c\udccf|\u2934|\u2935|[\u2190-\u21ff])/g
let regex = /^(?:[\u2700-\u27bf]|(?:\ud83c[\udde6-\uddff])2|[\ud800-\udbff][\udc00-\udfff]|[\u0023-\u0039]\ufe0f?\u20e3|\u3299|\u3297|\u303d|\u3030|\u24c2|\ud83c[\udd70-\udd71]|\ud83c[\udd7e-\udd7f]|\ud83c\udd8e|\ud83c[\udd91-\udd9a]|\ud83c[\udde6-\uddff]|[\ud83c[\ude01\uddff]|\ud83c[\ude01-\ude02]|\ud83c\ude1a|\ud83c\ude2f|[\ud83c[\ude32\ude02]|\ud83c\ude1a|\ud83c\ude2f|\ud83c[\ude32-\ude3a]|[\ud83c[\ude50\ude3a]|\ud83c[\ude50-\ude51]|\u203c|\u2049|[\u25aa-\u25ab]|\u25b6|\u25c0|[\u25fb-\u25fe]|\u00a9|\u00ae|\u2122|\u2139|\ud83c\udc04|[\u2600-\u26FF]|\u2b05|\u2b06|\u2b07|\u2b1b|\u2b1c|\u2b50|\u2b55|\u231a|\u231b|\u2328|\u23cf|[\u23e9-\u23f3]|[\u23f8-\u23fa]|\ud83c\udccf|\u2934|\u2935|[\u2190-\u21ff])$/g

console.log(regex.test("5️⃣")); // true - correct
console.log(regex.test("????")); // false - should be true
console.log(regex.test("????️")); // false - should be true
console.log(regex.test("⛅")); // true - correct
console.log(regex.test("????????")); // false - correct
这也很糟糕(第二次测试根据第一次测试改变?)
let regex = /^(?:[\u2700-\u27bf]|(?:\ud83c[\udde6-\uddff])2|[\ud800-\udbff][\udc00-\udfff]|[\u0023-\u0039]\ufe0f?\u20e3|\u3299|\u3297|\u303d|\u3030|\u24c2|\ud83c[\udd70-\udd71]|\ud83c[\udd7e-\udd7f]|\ud83c\udd8e|\ud83c[\udd91-\udd9a]|\ud83c[\udde6-\uddff]|[\ud83c[\ude01\uddff]|\ud83c[\ude01-\ude02]|\ud83c\ude1a|\ud83c\ude2f|[\ud83c[\ude32\ude02]|\ud83c\ude1a|\ud83c\ude2f|\ud83c[\ude32-\ude3a]|[\ud83c[\ude50\ude3a]|\ud83c[\ude50-\ude51]|\u203c|\u2049|[\u25aa-\u25ab]|\u25b6|\u25c0|[\u25fb-\u25fe]|\u00a9|\u00ae|\u2122|\u2139|\ud83c\udc04|[\u2600-\u26FF]|\u2b05|\u2b06|\u2b07|\u2b1b|\u2b1c|\u2b50|\u2b55|\u231a|\u231b|\u2328|\u23cf|[\u23e9-\u23f3]|[\u23f8-\u23fa]|\ud83c\udccf|\u2934|\u2935|[\u2190-\u21ff])$/g

console.log(regex.test("⛹????‍♂️")); // false
console.log(regex.test("⛅")); // true
let regex = /^(?:[\u2700-\u27bf]|(?:\ud83c[\udde6-\uddff])2|[\ud800-\udbff][\udc00-\udfff]|[\u0023-\u0039]\ufe0f?\u20e3|\u3299|\u3297|\u303d|\u3030|\u24c2|\ud83c[\udd70-\udd71]|\ud83c[\udd7e-\udd7f]|\ud83c\udd8e|\ud83c[\udd91-\udd9a]|\ud83c[\udde6-\uddff]|[\ud83c[\ude01\uddff]|\ud83c[\ude01-\ude02]|\ud83c\ude1a|\ud83c\ude2f|[\ud83c[\ude32\ude02]|\ud83c\ude1a|\ud83c\ude2f|\ud83c[\ude32-\ude3a]|[\ud83c[\ude50\ude3a]|\ud83c[\ude50-\ude51]|\u203c|\u2049|[\u25aa-\u25ab]|\u25b6|\u25c0|[\u25fb-\u25fe]|\u00a9|\u00ae|\u2122|\u2139|\ud83c\udc04|[\u2600-\u26FF]|\u2b05|\u2b06|\u2b07|\u2b1b|\u2b1c|\u2b50|\u2b55|\u231a|\u231b|\u2328|\u23cf|[\u23e9-\u23f3]|[\u23f8-\u23fa]|\ud83c\udccf|\u2934|\u2935|[\u2190-\u21ff])$/g;

console.log(regex.test("⛹")); // true
console.log(regex.test("⛅")); // false

有没有办法解决所有这些表情符号/unicode/regex 混乱?库/api是唯一的方法吗?他们是怎么做到的?

【问题讨论】:

见regex101.com/r/dQEURi/1 我个人认为维护一个正则表达式并不是一个好主意,除非你是从unicode specification 生成它。不过,已经有一些图书馆正在这样做。见emoji-regex 和emoji.json。 感谢 Wiktor Stribiżew,该正则表达式完美运行!我可以知道你是如何或从哪里得到这个的吗? 图书馆有时间和地点。恕我直言,这是其中之一。让其他人为您解决这个非常复杂的问题似乎远远超过了维护依赖项及其版本和文件大小的成本。 回复:“他们是如何做到的” Daniel Vickers 链接的库指的是:unicode.org/reports/tr29/#Default_Grapheme_Cluster_Table 【参考方案1】:

使用这个库:https://github.com/foliojs/grapheme-breaker 试试这个:

var str = "⛹?‍♂️";
var Grapheme = require('grapheme-splitter');
var splitter = new Grapheme();
console.log(splitter.splitGraphemes(str).length);

并且长度应该返回 1。

【讨论】:

这个库很棒,但它已经有一年不活跃了,但它的灵感来自最近活跃的github.com/foliojs/grapheme-breaker :) (我删除了我的旧评论,因为它有错误的链接) 谢谢,我也更新了答案中的链接。【参考方案2】:

表情符号在 UAX #51 中指定。 \pEmoji 属性应该可以工作,但不能。

努力去做。解析emoji-*.txt:

perl -C -lne'
    if (my ($c) = $_ =~ /^((?:(?:[[:xdigit:]]+ )|[[:xdigit:]]+\.\.)[[:xdigit:]]+)/) 
        if ($c =~ /\.\./)  # ranges
             my ($f, $t) = map  hex  split /\.\./, $c;
             print for map  chr  $f..$t;
         else  # sequences
             print join "", map  chr hex  split /\s+/, $c;
        
    
' emoji-*.txt

这为我们提供了所有表情符号的换行符列表。使用 Regexp::Assemble::Compressed,结果为

/^(?:[\u23EB\u23EC\u23F0\u2607-\u260D\u260F\u2610\u2616\u2617\u2619-\u261C\u261E\u261F\u2624\u2625\u2627-\u2629\u262B-\u262D\u2630-\u2637\u263B-\u263F\u2643-\u2647\u2654-\u265E\u2661\u2662\u2669-\u267A\u267C\u267D\u2680-\u2685\u2690\u2691\u269D-\u269F\u26A2-\u26A9\u26AC-\u26AF\u26B2-\u26BC\u26BF-\u26C3\u26C6\u26C7\u26C9-\u26CE\u26D5-\u26E8\u26EB-\u26EF\u26FB\u26FC\u26FE-\u2701\u2703\u2704\u2710\u2711\u2754\u2755\u2765-\u2767\u2795-\u2797\u1F000-\u1F003\u1F005-\u1F0CE\u1F0D0-\u1F0FF\u1F10D-\u1F10F\u1F16C-\u1F16F\u1F191-\u1F19A\u1F1AD-\u1F1E5\u1F201\u1F203-\u1F20F\u1F232-\u1F236\u1F238-\u1F23A\u1F23C-\u1F23F\u1F249-\u1F30C\u1F310-\u1F314\u1F316-\u1F31B\u1F31D-\u1F320\u1F322\u1F323\u1F32D-\u1F335\u1F337-\u1F377\u1F379-\u1F37C\u1F37E-\u1F384\u1F386-\u1F392\u1F394\u1F395\u1F39C\u1F39D\u1F3A0-\u1F3A6\u1F3A8-\u1F3AB\u1F3AF-\u1F3C1\u1F3C5\u1F3C8\u1F3C9\u1F3CF-\u1F3D3\u1F3E1-\u1F3EC\u1F3EE-\u1F3F2\u1F3F8-\u1F407\u1F409-\u1F414\u1F416-\u1F41E\u1F420-\u1F425\u1F427-\u1F43E\u1F440\u1F444\u1F445\u1F451\u1F452\u1F454-\u1F465\u1F479-\u1F47B\u1F47E-\u1F480\u1F484\u1F488-\u1F4A2\u1F4A4-\u1F4A9\u1F4AB-\u1F4AF\u1F4B1\u1F4B2\u1F4B4-\u1F4BA\u1F4BC-\u1F4BE\u1F4C0-\u1F4CA\u1F4CC-\u1F4D9\u1F4DB-\u1F4DE\u1F4E0-\u1F4E3\u1F4E7-\u1F4E9\u1F4EE-\u1F4F6\u1F4F8\u1F4FC\u1F4FF-\u1F507\u1F509-\u1F50C\u1F50E-\u1F511\u1F514-\u1F53D\u1F546-\u1F548\u1F54B-\u1F54E\u1F568-\u1F56E\u1F571\u1F572\u1F57B-\u1F586\u1F588\u1F589\u1F58E\u1F58F\u1F591-\u1F594\u1F597-\u1F5A3\u1F5A6\u1F5A7\u1F5A9-\u1F5B0\u1F5B3-\u1F5BB\u1F5BD-\u1F5C1\u1F5C5-\u1F5D0\u1F5D4-\u1F5DB\u1F5DF\u1F5E0\u1F5E4-\u1F5E7\u1F5E9-\u1F5EE\u1F5F0-\u1F5F2\u1F5F4-\u1F5F9\u1F5FB-\u1F60F\u1F611-\u1F644\u1F648-\u1F64A\u1F680-\u1F686\u1F688-\u1F68C\u1F68E-\u1F690\u1F692\u1F693\u1F695-\u1F697\u1F699-\u1F6A2\u1F6A4-\u1F6AC\u1F6AE-\u1F6B1\u1F6B3\u1F6B7\u1F6B8\u1F6BB\u1F6BD-\u1F6BF\u1F6C1-\u1F6CA\u1F6D0-\u1F6D4\u1F6D6-\u1F6DF\u1F6E6-\u1F6E8\u1F6EB-\u1F6EF\u1F6F1\u1F6F2\u1F6F4-\u1F6F8\u1F6FB-\u1F6FF\u1F774-\u1F77F\u1F7D5-\u1F7FF\u1F80C-\u1F80F\u1F848-\u1F84F\u1F85A-\u1F85F\u1F888-\u1F88F\u1F8AE-\u1F8FF\u1F90D\u1F90E\u1F910-\u1F917\u1F91D\u1F920-\u1F925\u1F927-\u1F92F\u1F93A\u1F940-\u1F945\u1F947-\u1F94B\u1F94D-\u1F970\u1F973-\u1F979\u1F97C-\u1F9B4\u1F9B7\u1F9BA\u1F9BC-\u1F9BF\u1F9C1-\u1F9CC\u1F9D0\u1F9E0-\u1FFFD\uE0020-\uE007F]|\u1F1F2[\u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[\u200D\u1F3FB-\u1F3FF]?|\u1F471[\u200D\u1F3FB-\u1F3FF]?|\u1F473[\u200D\u1F3FB-\u1F3FF]?|\u1F477[\u200D\u1F3FB-\u1F3FF]?|\u1F481[\u200D\u1F3FB-\u1F3FF]?|\u1F482[\u200D\u1F3FB-\u1F3FF]?|\u1F486[\u200D\u1F3FB-\u1F3FF]?|\u1F487[\u200D\u1F3FB-\u1F3FF]?|\u1F590[\uFE0E\uFE0F\u1F3FB-\u1F3FF]|\u1F645[\u200D\u1F3FB-\u1F3FF]?|\u1F646[\u200D\u1F3FB-\u1F3FF]?|\u1F647[\u200D\u1F3FB-\u1F3FF]?|\u1F64B[\u200D\u1F3FB-\u1F3FF]?|\u1F64D[\u200D\u1F3FB-\u1F3FF]?|\u1F64E[\u200D\u1F3FB-\u1F3FF]?|\u1F6A3[\u200D\u1F3FB-\u1F3FF]?|\u1F6B4[\u200D\u1F3FB-\u1F3FF]?|\u1F6B5[\u200D\u1F3FB-\u1F3FF]?|\u1F6B6[\u200D\u1F3FB-\u1F3FF]?|\u1F926[\u200D\u1F3FB-\u1F3FF]?|\u1F937[\u200D\u1F3FB-\u1F3FF]?|\u1F938[\u200D\u1F3FB-\u1F3FF]?|\u1F939[\u200D\u1F3FB-\u1F3FF]?|\u1F93D[\u200D\u1F3FB-\u1F3FF]?|\u1F93E[\u200D\u1F3FB-\u1F3FF]?|\u1F9B8[\u200D\u1F3FB-\u1F3FF]?|\u1F9B9[\u200D\u1F3FB-\u1F3FF]?|\u1F9CD[\u200D\u1F3FB-\u1F3FF]?|\u1F9CE[\u200D\u1F3FB-\u1F3FF]?|\u1F9CF[\u200D\u1F3FB-\u1F3FF]?|\u1F9D1[\u200D\u1F3FB-\u1F3FF]?|\u1F9D6[\u200D\u1F3FB-\u1F3FF]?|\u1F9D7[\u200D\u1F3FB-\u1F3FF]?|\u1F9D8[\u200D\u1F3FB-\u1F3FF]?|\u1F9D9[\u200D\u1F3FB-\u1F3FF]?|\u1F9DA[\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0F]|\u2744[\uFE0E\uFE0F]|\u2747[\uFE0E\uFE0F]|\u2757[\uFE0E\uFE0F]|\u27A1[\uFE0E\uFE0F]|\u2B50[\uFE0E\uFE0F]|\u2B55[\uFE0E\uFE0F]|\u3030[\uFE0E\uFE0F]|\u303D[\uFE0E\uFE0F]|\u3297[\uFE0E\uFE0F]|\u3299[\uFE0E\uFE0F]|\u1F004[\uFE0E\uFE0F]|\u1F21A[\uFE0E\uFE0F]|\u1F22F[\uFE0E\uFE0F]|\u1F587[\uFE0E\uFE0F]|\u1F5A5[\uFE0E\uFE0F]|\u1F5A8[\uFE0E\uFE0F]|\u1F5BC[\uFE0E\uFE0F]|\u1F5E1[\uFE0E\uFE0F]|\u1F5E3[\uFE0E\uFE0F]|\u1F5E8[\uFE0E\uFE0F]|\u1F5EF[\uFE0E\uFE0F]|\u1F5F3[\uFE0E\uFE0F]|\u1F6E9[\uFE0E\uFE0F]|\u1F6F0[\uFE0E\uFE0F]|\u1F6F3[\uFE0E\uFE0F]|\u1F1F4\u1F1F2?|\u1F1F6\u1F1E6?|\u1F1FD\u1F1F0?|\u1F46F\u200D?|\u1F93C\u200D?|\u1F9DE\u200D?|\u1F9DF\u200D?)$/u

【讨论】:

感谢您的解释,真的很有帮助!但是,我似乎无法使您生成的正则表达式工作,因为它在我尝试的任何地方都会出错。

以上是关于有没有办法检查 JS 中的字符串是不是是一个表情符号?的主要内容,如果未能解决你的问题,请参考以下文章

有没有办法限制 Android 软键盘只允许表情符号字符?

java处理数据库不支持的emoji表情符

PHP(或jQuery)有没有办法检查字符串是不是是人类可读的?

js 怎么计算一个字符串中包含多少个表情

有没有办法检查字符串是不是存在于另一个数据框中?

将表情字符转换成对应的图片 用js