Created
February 7, 2017 03:33
-
-
Save kewogc/9562b51753787e8dd60016c4ab711139 to your computer and use it in GitHub Desktop.
Javascript remove utf16 charters, emoji, special symbols and others
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
// remove emoji and all symbols from utf16 | |
String.prototype.strip_utf8only = function () { | |
function stringToCodePointArray(str) { | |
var codePoints = [], i = 0, charCode; | |
while (i < str.length) { | |
charCode = str.charCodeAt(i); | |
if ((charCode & 0xF800) != 0xD800) { // https://mathiasbynens.be/notes/javascript-encoding | |
codePoints.push(String.fromCharCode(charCode)); | |
} | |
++i; | |
} | |
return codePoints; | |
} | |
return stringToCodePointArray(this).join(""); | |
}; | |
"👽😀☂❤华み원❤𝌆 бла бла бла $¢-¥؋৲-৳૱௹฿៛₠-₵﷼﹩$¢-£¥-₩".strip_utf8only() | |
// => ☂❤华み원❤ бла бла бла $¢-¥؋৲-৳૱௹฿៛₠-₵﷼﹩$¢-£¥-₩ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment