문제

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" />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. ]]>

More precisely this:

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

and this:

]]>

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

도움이 되었습니까?

해결책

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 : link

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top