请看这个JsFiddle。
var target = "Thanks For Looking This Problem";
var phrase = ["anks", "for", "king T"];
for(var indx = 0; indx < phrase.length; indx ++)
{
target = target.replace(new RegExp(phrase[indx], "gi"), "~~~" + phrase[indx]+ "```");
}
我得到这个输出:Th~~~anks``` ~~~for``` Loo~~~king T```his Problem
但我需要这个输出:Th~~~anks``` ~~~For``` Loo~~~king T```his Problem
“为了”而不是“为了”