JavaScript 正则表达式,用于保留 span 标签并删除其他标签

javascripts regular expression to keep span tag and remove other

本文关键字:标签 删除 其他 span 保留 正则表达式 用于 JavaScript      更新时间:2023-09-26

谁能为我提供JavaScript中的正则表达式

输入:

<table><tr><td>hello<td/> <span class='xyz'>Hi all </span></tr></table>

输出:

hello<span class='xyz'>Hi all</span>

我可以在 Java 中成功使用以下表达式,但我不知道 JavaScript 中的等效正则表达式。

String newValue = input.replaceAll("<(?!''/?span)[^>]+>", "");

如果我在 js 中使用以下表达式

input.replace(/<(?!/?span)[^>]+>/g, ");

我得到的输出为

 hello Hi all</span>

但我要

hello<span class='xyz'>Hi all</span>

如果你真的想进入这个,这个网站可能会帮助你:http://www.txt2re.com/index-php.php3

我已经为你做了第一步:http://www.txt2re.com/index-javascript.php3?s=%3Ctable%3E%3Ctr%3E%3Ctd%3Ehello%3Ctd/%3E%20%3Cspan%20class=%27xyz%27%3EHi%20all%20%3C/span%3E%3C/tr%3E%3C/table%3E&-4&-19&-18&-11&-8&-1&-30&-25&-3&-7&-2