js实现敏感词过滤
敏感词过滤实现方法
基础字符串匹配法
使用字符串的 includes 或 indexOf 方法检查文本是否包含敏感词。适用于简单场景。
const sensitiveWords = ['敏感词1', '敏感词2', '敏感词3'];
const text = '这是一段包含敏感词1的文本';
function checkSensitiveWords(text, words) {
return words.some(word => text.includes(word));
}
console.log(checkSensitiveWords(text, sensitiveWords)); // true
正则表达式匹配法
使用正则表达式进行更灵活的匹配,支持模糊匹配和大小写忽略。
const sensitiveWords = ['敏感词1', '敏感词2', '敏感词3'];
const regex = new RegExp(sensitiveWords.join('|'), 'gi');
function filterText(text) {
return text.replace(regex, '*');
}
console.log(filterText('包含敏感词1和敏感词2的文本')); // 包含*和*的文本
字典树(Trie)优化法
对于大量敏感词,使用字典树结构提高匹配效率。

class TrieNode {
constructor() {
this.children = {};
this.isEnd = false;
}
}
class Trie {
constructor() {
this.root = new TrieNode();
}
insert(word) {
let node = this.root;
for (const char of word) {
if (!node.children[char]) {
node.children[char] = new TrieNode();
}
node = node.children[char];
}
node.isEnd = true;
}
}
function buildTrie(words) {
const trie = new Trie();
words.forEach(word => trie.insert(word));
return trie;
}
function filterWithTrie(text, trie) {
let result = [];
let i = 0;
const n = text.length;
while (i < n) {
let j = i;
let node = trie.root;
let lastMatch = -1;
while (j < n && node.children[text[j]]) {
node = node.children[text[j]];
if (node.isEnd) lastMatch = j;
j++;
}
if (lastMatch !== -1) {
result.push('*'.repeat(lastMatch - i + 1));
i = lastMatch + 1;
} else {
result.push(text[i]);
i++;
}
}
return result.join('');
}
const trie = buildTrie(['敏感词', '测试']);
console.log(filterWithTrie('这是一个敏感词测试', trie)); // 这是一个
性能优化建议
- 预处理敏感词字典,构建高效数据结构
- 对于长文本采用分块处理
- 考虑使用Web Worker避免阻塞主线程
- 对匹配结果进行缓存处理
注意事项
- 注意Unicode字符处理
- 考虑同音词、形近词等变体
- 处理边界情况如空字符串
- 考虑性能与准确性的平衡






