Replace a text emoji with a picture

  • 2020-03-30 02:41:21
  • OfStack

A common effect on weibo or forums is that when you insert an expression into the text field, you will see a string like [haha]. Here is a way to convert an expression string into a picture expression
<div id="test">abc</div> 

var face ={'[ Ha ha ]':'<img src="images/face/haha.gif" alt="" />','[ sweat ]':'<img src="images/face/liuhan.gif" alt="" />'} //See the complete qq expression at the end of this article

var reg = /[.+?]/g; 

var str = '[ Ha ha ]abc[ sweat ][ sweat ]'; //So here's the value of the text field that we got, and for brevity, we just used the string.

str = str.replace(reg,function(a,b){ 

return face[a]; 


document.getElementById('test').innerHTML = str; 

Qq expression package and corresponding face object:

Related articles: