如何将所有表情符号的 '\u' 代码与正则表达式匹配?
how to match '\u' codes for all the emojis with regexp?
我想找到匹配所有具有 \u 代码形式的表情符号的字符串。我正在尝试使用以下正则表达式,但无法正常工作。
/([\uE000-\uF8FF]|\uD83C[\uDC00-\uDFFF]|\uD83D[\uDC00-\uDFFF]|[\u2694-\u2697]|\uD83E[\uDD10-\uDD5D])/g
但是,它没有检测到。我要匹配得到
\ud83d\ude04\ud83d\ude04\ud83d\ude04\ud83d\ude04\ud83d\ude04\ud83d\ude04
这些类型的字符。
如果你想使用正则表达式匹配 \uXXXX
格式的表情符号,你可以使用 this Regex:
/\u[a-z0-9]{4}/gi
这是一个简单的演示:
const regex = /\u[a-z0-9]{4}/gi;
const str = `This is a pragraph \ud83d having some emojis like these ones:
\ude04
\ud83d
\ude04
Have you seen them?
\ud83d\ude04
Great!
`;
let m;
while ((m = regex.exec(str)) !== null) {
// This is necessary to avoid infinite loops with zero-width matches
if (m.index === regex.lastIndex) {
regex.lastIndex++;
}
// The result can be accessed through the `m`-variable.
m.forEach((match, groupIndex) => {
console.log(`Found match, group ${groupIndex}: ${match}`);
});
}
您编写的正则表达式将无法工作,因为您没有转义 \
。
我想找到匹配所有具有 \u 代码形式的表情符号的字符串。我正在尝试使用以下正则表达式,但无法正常工作。
/([\uE000-\uF8FF]|\uD83C[\uDC00-\uDFFF]|\uD83D[\uDC00-\uDFFF]|[\u2694-\u2697]|\uD83E[\uDD10-\uDD5D])/g
但是,它没有检测到。我要匹配得到
\ud83d\ude04\ud83d\ude04\ud83d\ude04\ud83d\ude04\ud83d\ude04\ud83d\ude04
这些类型的字符。
如果你想使用正则表达式匹配 \uXXXX
格式的表情符号,你可以使用 this Regex:
/\u[a-z0-9]{4}/gi
这是一个简单的演示:
const regex = /\u[a-z0-9]{4}/gi;
const str = `This is a pragraph \ud83d having some emojis like these ones:
\ude04
\ud83d
\ude04
Have you seen them?
\ud83d\ude04
Great!
`;
let m;
while ((m = regex.exec(str)) !== null) {
// This is necessary to avoid infinite loops with zero-width matches
if (m.index === regex.lastIndex) {
regex.lastIndex++;
}
// The result can be accessed through the `m`-variable.
m.forEach((match, groupIndex) => {
console.log(`Found match, group ${groupIndex}: ${match}`);
});
}
您编写的正则表达式将无法工作,因为您没有转义 \
。