正则表达式问题

yaray yaray 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区

// 字符串的中值 Linda 为变量
var content1 = "你好, <label style='background: blue; color: white'>Linda</label>";
var content2 = "你好, Linda";

求 content1 与 content2 互相转换正则表达式(为对应的字符串 着色/取消着色):
content2 --> content1
content1 --> content2

以下是本人写的(有错误,但不知道错在何处):
var key = "Linda";
var reg1 = eval("/^.*("+ key +").*$/");
var reg2 = /^<label style=\'background: blue; color: white\'>(.*)<\/label>$/;

content2.replace(reg1, "<label style='background: blue; color: white'>$1</label>");
content1.replace(reg2, "$1");

先感谢大家的回答!!
...全文
评论 4
收藏 已收藏
分享
返回顶部
yaray
JavaScript 87,983 成员 224,681 内容