Replace emoticons with emoji

女生的网名这么多〃 提交于 2019-12-02 04:28:47

问题


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.


回答1:


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/




回答2:


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

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



来源:https://stackoverflow.com/questions/29935513/replace-emoticons-with-emoji

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!