87,903
社区成员
发帖
与我相关
我的任务
分享
var emoticons = [
{ name: "微笑", icon: "App_Themes/Default/images/emoticons/0.gif" },
{ name: "呲牙", icon: "App_Themes/Default/images/emoticons/1.gif" }
];
var str = "[微笑[呲牙]";
str.replace(/\[[^\]]+?\]/g, function (word) {
var name = word.substring(1, word.length - 1);
for (var i = 0; i < emoticons.length; i++) {
if (name == emoticons[i].name)
return "<img src='" + emoticons[i].icon + "' />";
}
return word;
});
// 返回的字符串是:[微笑[呲牙] 没有替换后面的[呲牙]
// 想要的效果是:[微笑<img src='App_Themes/Default/images/emoticons/1.gif' />
var emoticons = [
{ name: "微笑", icon: "App_Themes/Default/images/emoticons/0.gif" },
{ name: "呲牙", icon: "App_Themes/Default/images/emoticons/1.gif" }
];
var str = "[微笑[呲牙]";
str.replace(/\[([^\]\[]+?)\]/g, function(a, b) {
for (var i = 0; i < emoticons.length; i++) {
if (b == emoticons[i].name) return "<img src='" + emoticons[i].icon + "' />";
}
return a;
});