Skip to content

Instantly share code, notes, and snippets.

@kewogc
Created February 7, 2017 03:33
Show Gist options
  • Save kewogc/9562b51753787e8dd60016c4ab711139 to your computer and use it in GitHub Desktop.
Save kewogc/9562b51753787e8dd60016c4ab711139 to your computer and use it in GitHub Desktop.
Javascript remove utf16 charters, emoji, special symbols and others
// 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