从字符串中清除CDATA [英] Clean away CDATA from string

查看:90
本文介绍了从字符串中清除CDATA的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用Javascript清除此文本块的一部分:

I need to clean away parts of this block of text using Javascript:

<![CDATA [< a href ="http://example.com/20.0.0.1/13902/cf085cef63511989576657751aad3cda.jpg" width ="3543" height ="2362"/&LoremIpsum只是印刷和排版行业的伪文本.自1500年代以来,Lorem Ipsum一直是行业的标准伪文本,当时一位不知名的打印机拿起一个厨房,将其打乱成一本样本书.它不仅生存了五个世纪,而且在电子排版方面也取得了飞跃,但基本上保持不变.它在1960年代流行,发行了包含Lorem Ipsum段落的Letraset表格,最近又发布了包括Alres PageMaker在内的桌面发行软件,包括Lorem Ipsum的版本.]]>

更精确的说:

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

,并且此:

]]>

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

Please note that the first part is not always the same (its from an XML-feed).

推荐答案

由于您要提取CDATA标记之间的文本,因此建议您使用简单的Regex来完成这项工作.

Since you want to extract the text between CDATA tags, I suggest you to use simple Regex to do the job.

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:链接

jsfiddle : link

这篇关于从字符串中清除CDATA的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆