示例;
var defaultEmphasisHandler = function(keyword, data){
var regex = RegExp("("+keyword.replace(/([.?*+^$[]\(){}|-])/g, "\$1")+")", 'ig');
return data.replace(regex, "<em style='color:red;'>$1</em>");
};
var s='我是中国人';
document.write(defaultEmphasisHandler('我',s));
如果输入中人
function EmphasisHandler(keyword, data) {
for(var i=0;i<keyword.length;i++){
var regex = RegExp("(" + keyword[i].replace(/([.?*+^$[]\(){}|-])/g, "\$1") + ")", 'ig');
data= data.replace(regex, "<em style='color:red;'>$1</em>");
}
return data;
}
var s='我是中国人';
document.write(EmphasisHandler('我中',s));