我正在尝试创建一个脚本,在文本中搜索模式并在找到的字符串周围包装标记.
$(".shop_attributes td").each(function () {
$(this).html(function(i, html) {
return html.replace(/E[0-9]{3,4}/g, "<strong>$1</strong>");
});
});
Run Code Online (Sandbox Code Playgroud)
这是我使用的代码,它确实找到我正在寻找的东西,但它实际上做的是生成一个内部$ 1的标签.我期望它做的是将它找到的字符串放入强标签.我在这做错了什么?
在使用之前,您需要捕获匹配项.使用括号:
$(".shop_attributes td").each(function () {
$(this).html(function(i, html) {
return html.replace(/(E[0-9]{3,4})/g, "<strong>$1</strong>");
});
});
Run Code Online (Sandbox Code Playgroud)