機密性の高い単語を含む配列sensitiveWordsを 作成します
_sendCommentHandle
関数内でfilterSensitiveWords
関数を使用してフィルタリングしますcontent(需要过滤的内容)
プロジェクトで、filter.js
というファイルを作成し、次のコードをそれに追加します。
//敏感词库:
const sensitiveWords = ['badword1', 'badword2', 'badword3'];
//创建过滤函数:
function filterSensitiveWords(text, sensitiveWords) {
const regexStr = sensitiveWords.map(word => `\\b${word}(s|es)?`).join("|");
const regex = new RegExp(regexStr, "gi");
return text.replace(regex, (match, p1) => {
return p1 ? "***" + p1 : "***";
});
}
export default {
sensitiveWords,
filterSensitiveWords,
};
輸入:
import filter from '@/filter';
フィルタリングを開始します。
content:需要过滤的内容,
filteredContent:过滤之后的内容
const filteredContent = filter.filterSensitiveWords(content, filter.sensitiveWords);
禁止語かどうかを判断する必要がある場合は、 「***」(センシティブワードを示す)が含まれているかどうかincludes()
をチェックする関数を使用しfilteredContent
、引き続き次のコードを追加します。
const hasSensitiveWord = this.filteredContent.includes("***");
センシティブワードがあるかどうかを判断してください