js实现敏感词过滤
敏感词过滤的实现方法
敏感词过滤是Web开发中常见的需求,可以通过多种方式在JavaScript中实现。以下是几种常用方法:
正则表达式匹配
使用正则表达式可以快速检测文本中是否包含敏感词。需要预先定义敏感词列表,并将其转换为正则表达式模式。
const sensitiveWords = ['badword1', 'badword2', 'badword3'];
const pattern = new RegExp(sensitiveWords.join('|'), 'gi');
function filterText(text) {
return text.replace(pattern, '*');
}
字典树(Trie)算法
对于大量敏感词,字典树是更高效的解决方案。它通过树形结构存储敏感词,减少重复匹配。
class TrieNode {
constructor() {
this.children = {};
this.isEnd = false;
}
}
class Trie {
constructor() {
this.root = new TrieNode();
}
insert(word) {
let node = this.root;
for (const ch of word) {
if (!node.children[ch]) {
node.children[ch] = new TrieNode();
}
node = node.children[ch];
}
node.isEnd = true;
}
search(word) {
let node = this.root;
for (const ch of word) {
if (!node.children[ch]) {
return false;
}
node = node.children[ch];
}
return node.isEnd;
}
}
function buildTrie(sensitiveWords) {
const trie = new Trie();
sensitiveWords.forEach(word => trie.insert(word));
return trie;
}
基于哈希表的快速查找
对于中小型敏感词库,可以使用简单的哈希表实现快速查找。
const sensitiveWords = new Set(['badword1', 'badword2', 'badword3']);
function hasSensitiveWord(text) {
const words = text.split(/\s+/);
return words.some(word => sensitiveWords.has(word.toLowerCase()));
}
结合多种方法的混合实现
实际应用中,通常会结合多种方法以提高过滤效果和性能。
function createWordFilter(sensitiveWords) {
const trie = buildTrie(sensitiveWords);
const regex = new RegExp(sensitiveWords.join('|'), 'gi');
return {
filter: function(text) {
// 先用正则快速过滤
let filtered = text.replace(regex, '*');
// 对剩余内容进行更精确的Trie检查
const words = filtered.split(/\s+/);
return words.map(word => {
return trie.search(word.toLowerCase()) ? '*' : word;
}).join(' ');
}
};
}
性能优化建议
- 敏感词列表应该预先加载并缓存,避免每次过滤都重新构建
- 对于大规模文本,可以考虑分批处理
- 使用Web Worker处理大量文本,避免阻塞主线程
- 定期更新敏感词库,保持过滤效果
以上方法可以根据实际需求选择或组合使用,平衡性能与准确性。对于特别严格的过滤需求,可能需要考虑服务端实现或专门的过滤服务。







