我需要使用Javascript清除文本块的部分内容:
<![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-feed)。
答案 0 :(得分:1)
由于您想要在CDATA标签之间提取文本,我建议您使用简单的Regex来完成这项工作。
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