replace

254 阅读1分钟

多次筛选 替换


/* 把富文本 文本提取出来 剔除css等 */
const reg = /(?<=>)[^>]+(?=<[\/]?\w+.*>)/g;
this.content = this.savaObj.personDetail.replace(reg, (s) => {
	// console.log(s);
    /* 纯文本内容 替换高亮 */
	return s.replace(
        /* linght 要筛选出来 替换的文本 */
	    new RegExp(linght, "gm"),
	    `<span style="color: #218BF7;font-weight: bold;">${linght}</span>`
	);
});

替换富文本内容 有个bug

就是 小程序 的ios 会出现白屏 (使用哪个 js的整个分包 的页面都会白屏) 如果用了 <= 就会出错打开白屏,仅IOS端出现,但是使用真机调试又没问题 developers.weixin.qq.com/community/d…

菜鸟教程 js之正则表达式修饰符/ig

    let strs = `<p style="width: 9rpx">123</p> <h1 style='width: 9rpx'>123</h1>`;
    var patt1 = /[^>]+(?=<[\/]?\w+.*>)/g;
    console.log(strs.match(patt1))
    // ["123", " ", "123"]