Replace emoticons with emoji

I've some text, that may include text emoticons (eg: ;), :(). I'd like to replace these with their corresponding emoji unicode characters (???? and ????) for the above example.

My intention is to then run twimoji on this text.

I know I can find-and-replace, but I'd rather use something already pre-build that does this already.

Any libraries that already implement this? I believe that the list of common emoticons is the largest bit of work here.

Answers:

Answer

Not sure if there's already a library out there that turns text emoticons to unicode characters but here's an example of how you could possibly implement it yourself.

 var map = {
   "<3": "\u2764\uFE0F",
   "</3": "\uD83D\uDC94",
   ":D": "\uD83D\uDE00",
   ":)": "\uD83D\uDE03",
   ";)": "\uD83D\uDE09",
   ":(": "\uD83D\uDE12",
   ":p": "\uD83D\uDE1B",
   ";p": "\uD83D\uDE1C",
   ":'(": "\uD83D\uDE22"
 };

 function escapeSpecialChars(regex) {
   return regex.replace(/([()[{*+.$^\\|?])/g, '\\$1');
 }

 document.getElementById('textarea').oninput = function() {
   for (var i in map) {
     var regex = new RegExp(escapeSpecialChars(i), 'gim');
     this.value = this.value = this.value.replace(regex, map[i]);
   }
 };

http://jsfiddle.net/04zv3ozh/18/

Answer

you can use this npm package that provide also a map of smiles and respective emoticons:

https://www.npmjs.com/package/smile2emoji

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.