javascript 如何使用javascript检测表情符号
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18862256/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to detect emoji using javascript
提问by user2699175
I need help for how to detect if an input contains a Japanese emoji/emoticon.
我需要有关如何检测输入是否包含日语表情符号/表情符号的帮助。
Currently my character set is charset=utf-8
. On inputting text, the user can enter Japanese characters/alpanumerics/symbols but if they insert an emoji, onsubmit
JavaScript will check if there is an emoji, error message will display.
目前我的字符集是charset=utf-8
. 在输入文本时,用户可以输入日文字符/字母数字/符号,但如果他们插入表情符号,onsubmit
JavaScript 将检查是否有表情符号,将显示错误消息。
I can't get this to work because I have no idea on how to detect an emoji in JavaScript?
我无法让它工作,因为我不知道如何在 JavaScript 中检测表情符号?
回答by lucas
You can use the following 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-\ude02]|\ud83c\ude1a|\ud83c\ude2f|\ud83c[\ude32-\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
If you just want to remove it from the string, you can do something like this.
如果你只是想从字符串中删除它,你可以做这样的事情。
function removeEmojis (string) {
var 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-\ude02]|\ud83c\ude1a|\ud83c\ude2f|\ud83c[\ude32-\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;
return string.replace(regex, '');
}
回答by iKBAHT
You can use a regular expression from this lib emoji-regex
您可以使用此 lib emoji-regex 中的正则表达式
回答by gidim
A simple function that returns true
if your string contains one or more emojis.
true
如果您的字符串包含一个或多个表情符号,则返回一个简单的函数。
function isEmoji(str) {
var ranges = [
'(?:[\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-\ude02]|\ud83c\ude1a|\ud83c\ude2f|[\ud83c[\ude32-\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])' // U+1F680 to U+1F6FF
];
if (str.match(ranges.join('|'))) {
return true;
} else {
return false;
}
}
回答by RiyasAbdulla
We can detect all list of surrogate pairs or the Emoji characters in a specific range. If the issue related with storing the input string to database like MySQL version before 5.5 we need to detect and remove all the surrogate pairs using the below regex
我们可以检测特定范围内的所有代理对列表或表情符号字符。如果问题与将输入字符串存储到 5.5 之前的 MySQL 版本等数据库有关,我们需要使用以下正则表达式检测并删除所有代理对
/([\uD800-\uDBFF][\uDC00-\uDFFF])/g.
回答by andorx
You can use regular expression to detect it in input text:
您可以使用正则表达式在输入文本中检测它:
/([\uE000-\uF8FF]|\uD83C[\uDF00-\uDFFF]|\uD83D[\uDC00-\uDDFF])/g
回答by Debashis Chowdhury
check the emoji like bellow ways
像波纹管一样检查表情符号
function getEmojiChars(text) {
console.log(text.match(/\ud83c[\udf00-\udfff]|\ud83d[\udc00-\ude4f]|\ud83d[\ude80-\udeff]/g)) ;
}
you will get the array of emoji in the text
您将在文本中获得表情符号数组