为什么匹配表情符号的正则表达式也匹配移动设备上的非表情符号( ' 和 " )?
Why is regex to match emojis also matching non-emoji symbols ( ' and " ) on mobile?
这是我在 Stack Overflow 上的第一个问题,希望得到帮助。
我使用正则表达式从用户输入的文本中检测表情符号。我用来匹配表情符号的正则表达式是:
(\u00a9|\u00ae|[\u2000-\u3300]|\ud83c[\ud000-\udfff]|\ud83d[\ud000-\udfff]|\ud83e[\ud000-\udfff])
但是,仅在移动设备上,它还会匹配各种非表情符号,包括 '
和 "
我已经尝试过其他用于表情符号匹配的正则表达式,但是 none 已经和这个一样简洁或者工作得很好。
谁能帮帮我?
要实时查看问题,请将 text/emojis/symbols 插入个人消息字段(使用移动设备):https://2050.cards/shop/birthday/eskimo-surprise/
示例输入:
不应该和不匹配:
Dear you, Happy Birthday.
应该匹配并且确实匹配:
Dear you , Thanks.
不应该但确实匹配(仅限移动设备):
Dear you, let's all "hang out"
正确的做法是 Unicode property escapes。
const r = /\p{Emoji}/u;
const a = [
'Dear you , Thanks. ',
'Dear you, Happy Birthday.',
`Dear you, let's all "hang out" • spend some €`
];
for (const s of a) {
const b = r.test(s);
console.log(b);
}
- Chrome 64 (2018)
- 边缘 79 (2019)
- Firefox 78(2020 年 6 月)
- 节点 10 (2018)
- 野生动物园 11.1 (2018)
这是我在 Stack Overflow 上的第一个问题,希望得到帮助。
我使用正则表达式从用户输入的文本中检测表情符号。我用来匹配表情符号的正则表达式是:
(\u00a9|\u00ae|[\u2000-\u3300]|\ud83c[\ud000-\udfff]|\ud83d[\ud000-\udfff]|\ud83e[\ud000-\udfff])
但是,仅在移动设备上,它还会匹配各种非表情符号,包括 '
和 "
我已经尝试过其他用于表情符号匹配的正则表达式,但是 none 已经和这个一样简洁或者工作得很好。
谁能帮帮我?
要实时查看问题,请将 text/emojis/symbols 插入个人消息字段(使用移动设备):https://2050.cards/shop/birthday/eskimo-surprise/
示例输入:
不应该和不匹配:
Dear you, Happy Birthday.
应该匹配并且确实匹配:
Dear you , Thanks.
不应该但确实匹配(仅限移动设备):
Dear you, let's all "hang out"
正确的做法是 Unicode property escapes。
const r = /\p{Emoji}/u;
const a = [
'Dear you , Thanks. ',
'Dear you, Happy Birthday.',
`Dear you, let's all "hang out" • spend some €`
];
for (const s of a) {
const b = r.test(s);
console.log(b);
}
- Chrome 64 (2018)
- 边缘 79 (2019)
- Firefox 78(2020 年 6 月)
- 节点 10 (2018)
- 野生动物园 11.1 (2018)