Last active
March 21, 2016 09:16
-
-
Save liunian/46192e7fe641cd9aeaea to your computer and use it in GitHub Desktop.
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
{ | |
decodeSpecialUnicode: function(str) { | |
var ptn = /\\u([0-f]{4})/ig; | |
return str.replace(ptn, function(match, group) { | |
return String.fromCharCode(parseInt(group, 16)); | |
}); | |
}, | |
encodeSpecialUnicode: function(str) { | |
// 转义特殊的 unicode 字符,如 Emoji 表情等,从而支持可能的某些情况下 api 不支持该范围 unicode 的问题 | |
var ptn = /[^\u0000-\uD7FF\uE000-\uFFFF]/g; | |
return str.replace(ptn, function(m) { | |
return Base.toUnicode(m); | |
}) | |
}, | |
toUnicode: function(str) { | |
var ret = ''; | |
for (var i = 0, l = str.length; i < l; i++) { | |
ret += '\\u' + ('000' + str[i].charCodeAt(0).toString(16)).substr(-4); | |
} | |
return ret; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
参考 String 中的
charAt
,charCodeAt
和codePointAt
对宽字符的处理