const keyword = '关键字'; let str = '把关键字换成蓝色'; const reg = new RegExp(`${keyword}`, 'g'); str = str.replace(reg, `<span style='color: #1B73E8;'>${keyword}</span>`);
// 插入div、span等 <div dangerouslySetInnerHTML={__html: str}></div>
效果展示:
React插入内容代码: dangerouslySetInnerHTML = {__html: 修改完带HTML代码的字符串}