把一个字符串里符合表情文字标签的地方全部替换为相应的图片的方法

1、表情数据:

var emotion =[
            {"name": "Expression_1","text": "[微笑]"},
            {"name": "Expression_2","text": "[撇嘴]"},
            {"name": "Expression_3","text": "[色]"},
            {"name": "Expression_4","text": "[发呆]"},
            {"name": "Expression_5","text": "[得意]"},
            {"name": "Expression_6","text": "[流泪]"},
            {"name": "Expression_7","text": "[害羞]"},
            {"name": "Expression_8","text": "[闭嘴]"},
            {"name": "Expression_9","text": "[睡]"},
            {"name": "Expression_10","text": "[大哭]"},
            {"name": "Expression_11","text": "[尴尬]"},
            {"name": "Expression_12","text": "[发怒]"},
            {"name": "Expression_13","text": "[调皮]"},
            {"name": "Expression_14","text": "[呲牙]"},
            {"name": "Expression_15","text": "[惊讶]"},
            {"name": "Expression_16","text": "[难过]"},
            {"name": "Expression_17","text": "[酷]"},
            {"name": "Expression_18","text": "[冷汗]"},
            {"name": "Expression_19","text": "[抓狂]"},
            {"name": "Expression_20","text": "[吐]"},
            {"name": "Expression_21","text": "[偷笑]"},
            {"name": "Expression_22","text": "[愉快]"},
            {"name": "Expression_23","text": "[白眼]"},
            {"name": "Expression_24","text": "[傲慢]"},
            {"name": "Expression_25","text": "[饥饿]"},
            {"name": "Expression_26","text": "[困]"},
            {"name": "Expression_27","text": "[恐惧]"},
            {"name": "Expression_28","text": "[流汗]"},
            {"name": "Expression_29","text": "[憨笑]"},
            {"name": "Expression_30","text": "[悠闲]"},
            {"name": "Expression_31","text": "[奋斗]"},
            {"name": "Expression_32","text": "[咒骂]"},
            {"name": "Expression_33","text": "[疑问]"},
            {"name": "Expression_34","text": "[嘘]"},
            {"name": "Expression_35","text": "[晕]"},
            {"name": "Expression_36","text": "[疯了]"},
            {"name": "Expression_37","text": "[衰]"},
            {"name": "Expression_38","text": "[骷髅]"},
            {"name": "Expression_39","text": "[敲打]"},
            {"name": "Expression_40","text": "[再见]"},
            {"name": "Expression_41","text": "[擦汗]"},
            {"name": "Expression_42","text": "[抠鼻]"},
            {"name": "Expression_43","text": "[鼓掌]"},
            {"name": "Expression_44","text": "[糗大了]"},
            {"name": "Expression_45","text": "[坏笑]"},
            {"name": "Expression_46","text": "[左哼哼]"},
            {"name": "Expression_47","text": "[右哼哼]"},
            {"name": "Expression_48","text": "[哈欠]"},
            {"name": "Expression_49","text": "[鄙视]"},
            {"name": "Expression_50","text": "[委屈]"},
            {"name": "Expression_51","text": "[快哭了]"},
            {"name": "Expression_52","text": "[阴险]"},
            {"name": "Expression_53","text": "[亲亲]"},
            {"name": "Expression_54","text": "[吓]"},
            {"name": "Expression_55","text": "[可怜]"},
            {"name": "Expression_56","text": "[菜刀]"},
            {"name": "Expression_57","text": "[西瓜]"},
            {"name": "Expression_58","text": "[啤酒]"},
            {"name": "Expression_59","text": "[篮球]"},
            {"name": "Expression_60","text": "[乒乓]"},
            {"name": "Expression_61","text": "[咖啡]"},
            {"name": "Expression_62","text": "[饭]"},
            {"name": "Expression_63","text": "[猪头]"},
            {"name": "Expression_64","text": "[玫瑰]"},
            {"name": "Expression_65","text": "[凋谢]"},
            {"name": "Expression_66","text": "[嘴唇]"},
            {"name": "Expression_67","text": "[爱心]"},
            {"name": "Expression_68","text": "[心碎]"},
            {"name": "Expression_69","text": "[蛋糕]"},
            {"name": "Expression_70","text": "[闪电]"},
            {"name": "Expression_71","text": "[炸弹]"},
            {"name": "Expression_72","text": "[刀]"},
            {"name": "Expression_73","text": "[足球]"},
            {"name": "Expression_74","text": "[瓢虫]"},
            {"name": "Expression_75","text": "[便便]"},
            {"name": "Expression_76","text": "[月亮]"},
            {"name": "Expression_77","text": "[太阳]"},
            {"name": "Expression_78","text": "[礼物]"},
            {"name": "Expression_79","text": "[拥抱]"},
            {"name": "Expression_80","text": "[强]"},
            {"name": "Expression_81","text": "[弱]"},
            {"name": "Expression_82","text": "[握手]"},
            {"name": "Expression_83","text": "[胜利]"},
            {"name": "Expression_84","text": "[抱拳]"},
            {"name": "Expression_85","text": "[勾引]"},
            {"name": "Expression_86","text": "[拳头]"},
            {"name": "Expression_87","text": "[差劲]"},
            {"name": "Expression_88","text": "[爱你]"},
            {"name": "Expression_89","text": "[NO]"},
            {"name": "Expression_90","text": "[OK]"},
            {"name": "Expression_91","text": "[爱情]"},
            {"name": "Expression_92","text": "[飞吻]"},
            {"name": "Expression_93","text": "[跳跳]"},
            {"name": "Expression_94","text": "[发抖]"},
            {"name": "Expression_95","text": "[怄火]"},
            {"name": "Expression_96","text": "[转圈]"},
            {"name": "Expression_97","text": "[磕头]"},
            {"name": "Expression_98","text": "[回头]"},
            {"name": "Expression_99","text": "[跳绳]"},
            {"name": "Expression_100","text": "[投降]"},
            {"name": "Expression_101","text": "[激动]"},
            {"name": "Expression_102","text": "[街舞]"},
            {"name": "Expression_103","text": "[献吻]"},
            {"name": "Expression_104","text": "[左太极]"},
            {"name": "Expression_105","text": "[右太极]"}
    ]

2、有一个字符串strCont="[微笑]不要说人家啦![撇嘴]人家好[害羞]啊"

3、批量替换方法:

/批量替换方法
function ReCont(strCont) {
    for (var i in emotion) {
        var item = emotion[i];
        var strimg=‘<img src="../image/emotion/‘+item.name+‘.png" width="24" height="24" align="absmiddle">‘;
        var reg=new RegExp("\\"+item.text,"g");
        //alert(reg);
        strCont=strCont.replace(reg, strimg);
        //alert(strCont);
    }
    return strCont;
}

然后使用:newContent=ReCont(strCont)就可以把表情标签更换为图片了。

另插入小说明:在js里插入特殊字符:

下面的表格列出了其余的特殊字符,这些特殊字符都可以使用反斜杠来添加到文本字符串中:

代码 输出

\’ 单引号

\" 双引号

\& 和号

\\ 反斜杠

\n 换行符

\r 回车符

\t 制表符

\b 退格符
时间: 2024-08-28 14:16:05

把一个字符串里符合表情文字标签的地方全部替换为相应的图片的方法的相关文章

判断一个字符串里是否含有某段字符?怎么截取一段字符?

写前端过程中遇到的最多的字符串操作莫过于 :判断一个字符串里是否含有某段字符 ,和 截取一段字符串. 字符串操作有很多方法,其实一般只要掌握以上两个就够用了,其他方法随他去吧,好,下面就以上两个方法讲解一下. 1.判断一个字符串是否含有某段字符,使用indexOf()方法: str.indexOf("参数1","参数2");参数1表示判断是否包含的小字符串, 参数2表示从左到有依次判断的起始位置,默认从0开始,str表示用于寻找的原字符串,ps:如果原字符串含多个判

Reverse Words in a String 翻转一个字符串里的单词顺序 @LeetCode

LeetCode新题,但是比较简单,直接用栈即可 Given an input string, reverse the string word by word. For example,Given s = "the sky is blue",return "blue is sky the". click to show clarification. Clarification: What constitutes a word?A sequence of non-sp

判断一个字符串里是否包含了另一个字符串

NSString *str1 = @"comeontom哈哈回复好 sad 分身乏术的发烧发烧的"; NSString *str2 = @"comeontom"; if ([str1 rangeOfString:str2].location != NSNotFound) { NSLog(@"str1包含str2"); }else{ NSLog(@"str1不包含str2"); } 判断一个字符串里是否包含了另一个字符串

把一个字符串里的所有单词的第一个字符转换为大写

//把一个字符串里的所有单词的第一个字符转换为大写  用到 了字符串方法split slice join charAt toUpperCase var str = 'ni hao ma!' function getUpper(str){ var words = str.split(' '); for(var i=0;i<words.length;i++){ words[i]=words[i].charAt(0).toUpperCase()+words[i].slice(1); } return

【C语言】字符串替换空格:实现一个函数,把字符串里的空格替换成“%20”

//字符串替换空格:实现一个函数,把字符串里的空格替换成"%20" #include <stdio.h> #include <assert.h> void replace(char *src) { assert(src); int OldLen = 0; //原字符串长度 int NewLen = 0; //新字符串长度 int BlackNum = 0; //空格数量 int NewBack = 0; //新字符串尾部 int OldBack = 0; //原

写一个函数,识别字符串是否符合python语法的变量名

2018年01月05日 09:07:19 阅读数:115 # 写一个函数,识别字符串是否符合python语法的变量名 # 导入关键字 import keyword key_word = keyword.kwlist def python_grammar(num): # 判断输入的空字符 if num == "": print("不符合python命名规则") return count = 0 while count < len(num): # 判断字符串的所有

从字符串里提取一个列表(数组)

var santent='this is one santent. this is a santent with a list of items:'+'cherries,oranges,apples,bananas. that was the list of items.';  //创建一个字符串 var start = santent.indexOf(':');  //找到要提取列表开始位置 var end = santent.indexOf('.',start+1); //找到要提取列表的结

在Python中使用正则表达式去掉字符串里的html标签

有时候会获得一些带html标签的字符串,需要把html标签去掉,获得干净的字符串,这时候可以使用正则表达式. 代码如下: import re htmeString = '''    <ul id="TopNav">        <li><a href="/EditPosts.aspx" id="TabPosts">随笔</a></li>        <li><a h

windows客户端开发--如何测量一个字符串显示的物理长度

首先需要说明的是,我所说的字符串的长度,不是string的length,也不是string的size.我指的是显示的长度,即物理长度. 缘由: 之所以要提到这个,是因为遇到了一些问题. 再使用duilib进行开发时,发现label控件不能自适应宽度. 思考: 这显示是这个库的一个不足,但是我们可以试图去修改一下这个库.但是本着开源的精神,或许这个库的设计初衷就是label不能自适应字符串的宽度呢? 所有只有走第二条路,我们首先获得要显示字符串的宽度width值,然后把这个label的宽度设置为w