赞
踩
_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("***");
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。