根据指定字符串在字符串(想要替换的字符串)中找到并替换

qing1224 / 2024-10-11 / 原文

removeFun(description) {

        description = description.replace(/<\/?[^>]*>/g, ''); // 去除HTML tag 如果后端返回的数据或者处理的数据不带标签 不用写这一步

 

————以上都是去除 后端返回的 "<span class="unable-analyse">bad input path</span>" 这种格式————————————————

        var reg = new RegExp('(' + description + ')', 'g'); //要替换的

        //前面是替换的字符串 后面是新的字符串

        var newstr = this.errorsContent.failReason.replace(reg, 替换的字符串this.errorsContent.highLightKey );

        console.log(newstr)//替换完成的字符串

        如果想要根据替换的字符串来出现高亮的话 可以这样写

        this.errorsContent.failReason.replace(reg, `<span class="Highlight">${111}</span>` );

},

//方法
 removeFun(description) { // 根据特定关键字展示高亮 
            description = description.replace(/<\/?[^>]*>/g, ''); // 去除HTML tag
            var reg = new RegExp('(' + description + ')', 'g');
            var newstr = this.data.replace(reg, description);
            var newstr2 = this.data2.replace(reg, description);
            this.data  = newstr;
            this.data2 = newstr2;
        },

 

 

样式中    

   >>>.Highlight{

        color:red;

        }