Created
November 1, 2022 06:05
-
-
Save Dnevend/a778b0102c5235a96464aac6ecc3f753 to your computer and use it in GitHub Desktop.
emojiTranslateInJavascript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 表情转码 | |
utf16toEntities(str) { | |
var patt = /[\ud800-\udbff][\udc00-\udfff]/g; | |
// 检测utf16字符正则 | |
str = str.replace(patt, function (char) { | |
var H, L, code; | |
if (char.length === 2) { | |
H = char.charCodeAt(0); | |
// 取出高位 | |
L = char.charCodeAt(1); | |
// 取出低位 | |
code = (H - 0xD800) * 0x400 + 0x10000 + L - 0xDC00; | |
// 转换算法 | |
return "&#" + code + ";"; | |
} else { | |
return char; | |
} | |
}); | |
return str; | |
}, | |
//表情解码 | |
uncodeUtf16(str) { | |
var reg = /\&#.*?;/g; | |
var result = str.replace(reg, function (char) { | |
var H, L, code; | |
if (char.length == 9) { | |
code = parseInt(char.match(/[0-9]+/g)); | |
H = Math.floor((code - 0x10000) / 0x400) + 0xD800; | |
L = (code - 0x10000) % 0x400 + 0xDC00; | |
return unescape("%u" + H.toString(16) + "%u" + L.toString(16)); | |
} else { | |
return char; | |
} | |
}); | |
return result; | |
}, |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment