var filterWord={
words:"",
tblRoot:{},
//敏感詞檔案
file:"sensitiveWords.txt", //載入敏感片語
load:function (file,callback) {
file=file||this.file;
var objHttp;
if (window.ActiveXObject) {
objHttp = new ActiveXObject("Microsoft.XMLHTTP");
}else {
objHttp = new XMLHttpRequest();
objHttp.overrideMimeType("text/xml");
} objHttp.onreadystatechange = function () {
if (objHttp.readyState != 4)
return;
this.words = objHttp.responseText;
callback(objHttp.responseText);
}; objHttp.open("GET", file, true);
objHttp.send(null);
}, //將關鍵字生成一顆樹
makeTree:function (callback) {
if(this.words==""){
this.load(this.file,function (words) {
var strKeys = words;
var arrKeys = strKeys.split("");
var tblCur = this.tblRoot = {};
var key; for (var i = 0, n = arrKeys.length; i < n; i++) {
key = arrKeys[i];
//完成當前關鍵字
if (key == ';'){
tblCur.end = true;
tblCur = this.tblRoot;
continue;
}
//生成子節點
if (key in tblCur)
tblCur = tblCur[key];
else
tblCur = tblCur[key] = {};
} //最後一個關鍵字沒有分割符
tblCur.end = true;
callback(this.tblRoot);
});
}else{
callback(this.tblRoot);
}
}, //標記出內容中敏感詞的位置
searchWords:function (content,root) {
var tblCur,p, v,i = 0,arrMatch = [];
var n = content.length;
while (i < n) {
tblCur = root;
p = i;
v = 0; for (; ;) {
if (!(tblCur = tblCur[content.charAt(p++)])) {
i++;
break;
}
//找到匹配敏感字
if (tblCur.end)
v = p;
}
//最大匹配
if (v){
arrMatch.push(i - 1, v);
i = v;
}
}
return arrMatch;
}, //標記敏感字
handle:function (strContent) {
var mid,arrMatch,strHTML,arrHTML = [],p = 0;
this.makeTree(function (data) {
arrMatch = filterWord.searchWords(strContent,data);
for (var i = 0, n = arrMatch.length; i < n; i += 2) {
mid = arrMatch[i];
arrHTML.push(strContent.substring(p, mid),
"<em>",
strContent.substring(mid, p = arrMatch[i + 1]),
"</em>");
}
arrHTML.push(strContent.substring(p)); strHTML = arrHTML.join("").replace(/\n/g, "<br>");
console.log(strHTML);
});
}
};

使用方式:

filterWord.handle("徐航撒颯颯阿薩颯颯劉孝朋啊啊撒颯颯")