gpt4 book ai didi

javascript - 从字符串中清除 CDATA

转载 作者:行者123 更新时间:2023-12-03 12:38:12 26 4
gpt4 key购买 nike

我需要使用 Javascript 清除该文本 block 的部分内容:

<![CDATA[<a href="http://example.com/20.0.0.1/13902/cf085cef63511989576657751aad3cda.jpg" width="3543" height="2362" />Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum. ]]>

更准确地说:

<![CDATA[<a href="http://example.com/20.0.0.1/13902/cf085cef63511989576657751aad3cda.jpg" width="3543" height="2362" />

还有这个:

]]>

请注意,第一部分并不总是相同的(来自 XML 源)。

最佳答案

由于您想要提取 CDATA 标记之间的文本,我建议您使用简单的正则表达式来完成这项工作。

function removeCDATA(str) {
var pattern = new RegExp(/\<!\[CDATA\[.*?\/>(.*?)\]\]\>/);
var res = pattern.exec(str)[1];
return res;
}

alert(removeCDATA('<![CDATA[<a href="http://example.com/20.0.0.1/13902/cf085cef63511989576657751aad3cda.jpg" width="3543" height="2362" />Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry\'s standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum. ]]>'))

jsfiddle:link

关于javascript - 从字符串中清除 CDATA,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23634491/

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