表情与unicode码互转
表情转unicode
const str = '笑脸😋桃心❤🙏拜托💤睡觉😀😃😄😁' const emojiReg = /[\uD83C|\uD83D|\uD83E][\uDC00-\uDFFF][\u200D|\uFE0F]|[\uD83C|\uD83D|\uD83E][\uDC00-\uDFFF]|[0-9|*|#]\uFE0F\u20E3|[0-9|#]\u20E3|[\u203C-\u3299]\uFE0F\u200D|[\u203C-\u3299]\uFE0F|[\u2122-\u2B55]|\u303D|[\A9|\AE]\u3030|\uA9|\uAE|\u3030/gi; const emojiReg2 = /[\u{1f300}-\u{1f5ff}\u{1f900}-\u{1f9ff}\u{1f600}-\u{1f64f}\u{1f680}-\u{1f6ff}\u{2600}-\u{26ff}\u{2700}-\u{27bf}\u{1f1e6}-\u{1f1ff}\u{1f191}-\u{1f251}\u{1f004}\u{1f0cf}\u{1f170}-\u{1f171}\u{1f17e}-\u{1f17f}\u{1f18e}\u{3030}\u{2b50}\u{2b55}\u{2934}-\u{2935}\u{2b05}-\u{2b07}\u{2b1b}-\u{2b1c}\u{3297}\u{3299}\u{303d}\u{00a9}\u{00ae}\u{2122}\u{23f3}\u{24c2}\u{23e9}-\u{23ef}\u{25b6}\u{23f8}-\u{23fa}]/ug; const strRes = str.replace(emojiReg, match => `[u+${match.codePointAt(0).toString(16)}]`) console.log(strRes)
unicode转表情
export default function Name(v) { if(v) { let reg = v.match(/(\[u+[a-zA-z\+0-9].*?\])/gim) if(reg !== null){ let str = v reg.map((item) => { let unicode = item.replace('[u+', '0x').replace(']', ''); let emjo = String.fromCodePoint(unicode) str = str.replace(item, emjo) }) return str } else { return v } } else { return v } }