超详细和完整的js正则ubb表情图片替换处理代码
//表情的基本数据var FaceData =function(){
var data =
[
["微笑",":)","wx","0.gif"],
["撇嘴",":~","pz","1.gif"],
["色",":*","se","2.gif"],
["发呆",":|","fd","3.gif"],
["得意","8-)","dy","4.gif"],
["流泪",":<","ll","5.gif"],
["害羞",":$","hx","6.gif"],
["闭嘴",":X","bz","7.gif"],
["睡",":Z","shui","8.gif"],
["大哭",":'(","dk","9.gif"],
["尴尬",":-|","gg","10.gif"],
["发怒",":@","fn","11.gif"],
["调皮",":P","tp","12.gif"],
["呲牙",":D","cy","13.gif"],
["惊讶",":0","jy","14.gif"],
["难过",":(","ng","15.gif"],
["酷",":+","kuk","16.gif"],
["冷汗","--b","lengh","17.gif"],
["抓狂",":Q","zk","18.gif"],
["吐",":T","tu","19.gif"],
["偷笑",";P","tx","20.gif"],
["可爱",";-D","ka","21.gif"],
["白眼",";d","baiy","22.gif"],
["傲慢",";o","am","23.gif"],
["饥饿",":g","jie","24.gif"],
["困","|-)","kun","25.gif"],
["惊恐",":!","jk","26.gif"],
["流汗",":L","lh","27.gif"],
["憨笑",":>","hanx","28.gif"],
["大兵",":",";db","29.gif"],
["奋斗",";f","fendou","30.gif"],
["咒骂",":-S","zhm","31.gif"],
["疑问","?","yiw","32.gif"],
["嘘",";x","xu","33.gif"],
["晕",";x","yun","34.gif"],
["折磨",":-S","zhm","35.gif"],
["衰",";!","shuai","36.gif"],
["骷髅","!!!","kl","37.gif"],
["敲","xx","qiao","38.gif"],
["再见","bye","zj","39.gif"],
["擦汗","wipe","ch","40.gif"],
["抠鼻","dig","kb","41.gif"],
["鼓掌","handclap","gz","42.gif"],
["糗大了","&-(","qd","43.gif"],
["坏笑","B-)","huaix","44.gif"],
["左哼哼","<@","zhh","45.gif"],
["右哼哼","@<","yhh","46.gif"],
["哈欠",":-0","hq","47.gif"],
["鄙视",">-|","bs","48.gif"],
["委屈","P-(","wq","49.gif"],
["快哭了",":'|","kk","50.gif"],
["阴险","X-)","yx","51.gif"],
["亲亲",":*","qq","52.gif"],
["吓","@x","xia","53.gif"],
["可怜","8*","kel","54.gif"],
["菜刀","pd","cd","55.gif"],
["西瓜","<w>","xig","56.gif"],
["啤酒",":beer","pj","57.gif"],
["篮球","basketb","lq","58.gif"],
["乒乓","oo","pp","59.gif"],
["咖啡","coffee","kf","60.gif"],
["饭","eat","fan","61.gif"],
["猪头","pig","zt","62.gif"],
["玫瑰","rose","mg","63.gif"],
["凋谢","fade","dx","64.gif"],
["示爱","showlove","sa","65.gif"],
["爱心","heart","xin","66.gif"],
["心碎","break","xs","67.gif"],
["蛋糕","cake","dg","68.gif"],
["闪电","li","shd","69.gif"],
["炸弹","bome","zhd","70.gif"],
["刀","kn","dao","71.gif"],
["足球","footb","zq","72.gif"],
["瓢虫","ladybug","pch","73.gif"],
["便便","易做图","bb","74.gif"],
["月亮","moom","yl","75.gif"],
["太阳","sun","ty","76.gif"],
["礼物","gift","lw","77.gif"],
["拥抱","hug","yb","78.gif"],
["强","strong","qiang","79.gif"],
["弱","weak","ruo","80.gif"],
["握手","share","ws","81.gif"],
["胜利","v","shl","82.gif"],
["抱拳","@)","bq","83.gif"],
["勾引","jj","gy","84.gif"],
["拳头","@@","qt","85.gif"],
["差劲","bad","cj","86.gif"],
["爱你","loveu","aini","87.gif"],
["NO","no","bu","88.gif"],
["OK","OK","hd","89.gif"]
];
return data;
}
//
function getReplaceFace(code){
for(var i=0;i<FaceData().length;i++){
var arr = FaceData()[i];
if( code == arr[3]){
return "[FACE]"+arr[0]+"[/FACE]";
}
}
}
//通过编码得到表情gif文件
function getFaceGIFByCode(code){
for(var i=0;i<FaceData().length;i++){
var arr = FaceData()[i];
if( arr[0] == code || arr[1] ==code || arr[2] ==code ){
return "<img alt=\"\" src=\"http:\/\/face.zhaoxi.org\/emo"+arr[3]+"\" border=\"0\" />";
}
}
}
//<img alt=\"\" src=\"..\/plugins\/emoticons\/(\d+.gif)\" border=\"0\" \/> 替换成 [face][/face]
function imgReplaceFace(str){
var arr;
var src = str;
var re = /<img alt=\"\" src=\"http:\/\/face.zhaoxi.org/emo\/(\d+.gif)\" border=\"0\" \/>/g; //创建正则表达式对象。
while( (arr = re.exec(str)) != null ) {
var s = getReplaceFace(arr[1]);
src = src.replace(/<img alt=\"\" src=\"http:\/\/face.zhaoxi.org\/emo\/(\d+.gif)\" border=\"0\" \/>/,s);
}
return src;
}
//[face][/face] 替换成 <img alt=\"\" src=\"http:\/\/face.zhaoxi.org\/emo\/(\d+.gif)\" border=\"0\" \/>
function faceReplaceImg(str){
var arr;
var src = str;
var re = /\[(FACE|face)\]([^[\/FACE]+)\[\/(FACE|face)\]/g; //创建正则表达式对象。
while( (arr = re.exec(str)) != null ) {
var s = getFaceGIFByCode(arr[2]);
src = src.replace(/\[(FACE|face)\]([^[\/FACE]+)\[\/(FACE|face)\]/,s);
}
return src;
}