我有一块服务器生成的html(我不能直接编辑html,所以修改必须是javascript/jquery)&我需要去掉一个文本+ a可变数字,但留下一切。这里是我的html:在类中的字符串中替换文本+通配符号
<td>
<font class="carttext">
[Specifications:30][Specifications:
<br> These are other specifications: here & here
<br>And even more: because life is hard]
</font>
</td>
注意,[Specifications:30]
可能是[Specifications:40]
或[Specifications:90]
或[Specifications:120]
等,却总是与[Specifications:
启动,并与可变数字结尾& ]
这是我的非工作,但-best省力的jQuery:
var cartText = $(document.getElementsByClassName("carttext"));
cartText.html(function (index, html) {
return html.replace("[Specifications:" + /[0-9]+\]/, '');
});
也试过:
var cartText = $(document.getElementsByClassName("carttext"));
cartText.html(function (index, html) {
return html.replace("[Specifications:" + /d +\]/, '');
});
我在carttext
类"[Specifications:"
出现了多次,所以我只是想在字符串"[Specificaitons:(variable number here)"
UPDATE剥离的发生:我试图剥离不只是数量,但也[Specifications:
,使得:
<font class="carttext">
[Specifications:30][Specifications: <br> These are other
specifications: here & here <br>And even more: because life is hard]
</font>
变得
<font class="carttext">
[Specifications:<br> These are other specifications: here & here
<br>And even more: because life is hard]
</font>
对不起,以前没有指定
'html.replace(/ \ [规格:\ S * \ d + \] /克, '');'。 'g'是一个全局标志 - 允许regex多次匹配字符串。 – jperezov
您不能合并字符串和正则表达式。这是你的问题。 – epascarello