我正在尝试制作一个锚标签删除功能
I am trying to make an anchor tag removal function
我尝试了MS Word的VBA宏(我目前使用的PC没有太多东西),结果不是很好,所以我用Firefox控制台尝试了它。
这是我当前的代码:
function stripAnchorTags(html) {
return html.replace(/'<a.*'>.*'<'/a'>/, '');
}
console.log(stripAnchorTags('<a href="image location" style="margin-left: 1em; margin-right: 1em;"><img src="image location" border="0" height="400" width="640"></a></div><br><a href="image location" style="margin-left: 1em; margin-right: 1em;"><img src="image location" border="0" height="400" width="640"></a></div>'));
现在,我想要的输出是
<img src="image location" border="0" height="400" width="640"></div><br><img src="image location" border="0" height="400" width="640"></div>
但取而代之的是,我只得到</div>
我不知道出了什么问题。
试试这个:
- 您需要使用"g"替换所有实例。
-
您正在做的是删除标签之间的所有内容。
function stripAnchorTags(html) { HTML = html.replace(/''/g, ''); HTML = html.replace(/'']*>/g, ''); 返回 HTML;}
===== 编辑 =====
单行:
function stripAnchorTags(html) {
return html.replace(/'<a.*?'>(.*?)'<'/a'>/g, ''$1');
}
相关文章: