采用DFA算法实现字库屏蔽规则

//需要过滤的字库
let filterWordsArr = '广告|下载|邀请|邀请码';
//切割字库成数组
filterWordsArr = filterWordsArr .slice(1, filterWordsArr.length - 1).split('|');
let filterWords = {};
//处理字符串,将字符串切割使用对象递归嵌套存储
function dealWords(wordObj, insertString) {
    if (insertString && insertString.length > 0) {
        //获取第一个字符
        let firstWord = insertString[0].toLocaleLowerCase();
        //获取剩余字符
        let remainStr = insertString.slice(1, insertString.length);
        if (!wordObj[firstWord]) {
            wordObj[firstWord] = {};
        }
        if (remainStr.length > 0) {
            wordObj[firstWord] = dealWords(wordObj[firstWord], remainStr);
        }
        else {
            wordObj[firstWord].isEnd = true;
        }
        return wordObj;
    }
    return wordObj;
}
filterWordsArr.forEach((e) => {
    dealWords(filterWords, e);
});

/**
 * 提取字符串里面符合规则的文字,并返回数组,都不符合会返回空数组
 */
function checkStr(str) {
    let result = [];
    let stack = [];
    let rule = filterWords;
    //状态,true表面传入字符串符合规则
    for (let i = 0; i < str.length; i++) {
        let ch = str.charAt(i).toLocaleLowerCase();
        let item = rule[ch];
        if (!item) {
            i = i - stack.length;
            stack = [];
            rule = filterWords;
        }
        else if (item.isEnd) {
            stack.push(ch);
            result.push(stack.join(""));
            stack = [];
            rule = filterWords;
        }
        else {
            stack.push(ch);
            rule = item;
        }
    }
    return result;
}

/**
 *
 * @param str    //要替换的字符串
 * @param replaceWords //替换成的字符
 */
function getReplaceWord(str, replaceWords) {
    let replaceStr = '';
    for (let i = 0; i < str.length; i++) {
        replaceStr += replaceWords;
    }
    return replaceStr;
}

/**
 * 将符合过滤规则的字符替换成*号
 * @param str           //传入要过滤的字符串
 * 如 filterStr('我的邀请码')  =》 返回   "我的**码"
 */
function filterStr(str) {
    str = str.toLocaleLowerCase();
    let replaceWord = '*';
    let filterWordList = checkStr(str);
    if (filterWordList && filterWordList.length > 0) {
        filterWordList.forEach(e => {
                let replaceWords = getReplaceWord(e, replaceWord);
                str = str.replace(e, replaceWords);    
        });
    }
    return str;
}
    原文作者:8239e604d437
    原文地址: https://www.jianshu.com/p/d21e36d009a2
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞