gpt4 book ai didi

javascript - 如何将所有 html 标签从 替换为\n\n [使用正则表达式 (JavaScript)]

转载 作者:行者123 更新时间:2023-11-28 12:10:05 26 4
gpt4 key购买 nike

如何替换 <anything> 中的所有 HTML 标签至\n<anything></anything><anything>\n

var text = "<anything>welcome</anything><anything>Hello</anything>";

结果

var text = "\n<anything>welcome</anything>\n\n<anything>Hello</anything>\n";

此代码将帮助您(匹配所有标签)

</?\w+((\s+\w+(\s*=\s*(?:".*?"|'.*?'|[^'">\s]+))?)+\s*|\s*)/?>

最佳答案

您可以在没有正则表达式的情况下美化 xml:

var text = "<anything>welcome</anything><anything>Hello</anything>";
var xml = new XML("<root>" + text + "</root>");
console.log(xml.children().toXMLString());

输出:

<anything>welcome</anything>
<anything>Hello</anything>

关于javascript - 如何将所有 html 标签从 <anything> 替换为\n<anything>\n [使用正则表达式 (JavaScript)],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3609761/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com