1 前言javascript
因爲帶emoj表情的暱稱沒法存儲在mysql character_set_server= utf8模式下,按照參考文章[1],改爲utf8mb4,無效(可能使用方法不對)。html
整體思路是,把暱稱過濾emoj表情,獲得過濾後的暱稱。java
2 代碼node
function filterNicknameWithEmoj(nickname){ var regStr = /[\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/ig; var nickname_filter=""; //regStr.test(nickname)會一次成功一次失敗,待排查是否和regStr寫法有關 if(regStr.test(nickname)){ nickname_filter = nickname.replace(regStr,""); nickname_filter = removeBlank(nickname_filter); return nickname_filter; } return nickname; } function removeBlank(str){ str = str.trim(); var ret = ""; for(var i = 0; i < str.length; i++){ if(str[i] != ' '){ ret+=str[i]; } } return ret; }
正則表達式還有其它表示方法,這個正則表達式能夠過濾ios10+所有表情包[2]mysql
3 參考ios
[1] 經過node.js保存emoji到mysql正則表達式
[2] https://www.cnblogs.com/zt-blog/p/6773854.htmlsql
[3] https://segmentfault.com/q/1010000005840528segmentfault
[4] https://segmentfault.com/q/1010000011875872/a-1020000011876055微信