Skip to content

Instantly share code, notes, and snippets.

@icetee
Last active June 4, 2017 18:42
Show Gist options
  • Save icetee/b7f69ed4814280ebf263760c6873eb25 to your computer and use it in GitHub Desktop.
Save icetee/b7f69ed4814280ebf263760c6873eb25 to your computer and use it in GitHub Desktop.
Change unicode Emojis to UTF-32 code.
<div id="emojiReplaceDemo"></div>
<script type="text/javascript">
(function($) {
$(document).ready(function() {
var content = searchEmojis("🙊", function(match) {
return '<img class="emojioneemoji" src="https://cdnjs.cloudflare.com/ajax/libs/emojione/2.1.4/assets/png/'+ uni2utf32(match) +'.png" alt="'+ match +'" />';
});
$('#emojiReplaceDemo').html(content);
});
})(jQuery);
</script>
/* ReplaceEmojis @icetee - https://gist.github.com/icetee/b7f69ed4814280ebf263760c6873eb25 | MIT */
function uni2utf32(str) {
var utf16 = [],
utf32 = "";
for (var i = 0; i < str.length; i++) {
utf16.push( ('0x' + str.charCodeAt(i).toString(16).toUpperCase()) - 0 );
}
utf32 = (utf16[0] - 0xD800) * 0x400 + utf16[1] - 0xDC00 + 0x10000;
return utf32.toString(16);
}
function searchEmojis(str, cb) {
if (!str) str = '';
var pattern = /([\uD800-\uDBFF][\uDC00-\uDFFF])/g,
is_matched = str.match(pattern);
if (typeof cb !== 'function' || !is_matched) return str;
return str.replace(pattern, function (match) {
return cb(match);
});
}
/* ReplaceEmojis @icetee - https://gist.github.com/icetee/b7f69ed4814280ebf263760c6873eb25 | MIT */
function uni2utf32(d){var a=[],c="";for(var b=0;b<d.length;b++){a.push(("0x"+d.charCodeAt(b).toString(16).toUpperCase())-0)}c=(a[0]-55296)*1024+a[1]-56320+65536;return c.toString(16)}function searchEmojis(d,a){if(!d){d=""}var c=/([\uD800-\uDBFF][\uDC00-\uDFFF])/g,b=d.match(c);if(typeof a!=="function"||!b){return d}return d.replace(c,function(e){return a(e)})};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment