1

I'm using angularjs to fetch json data from a rest webservice.

Problem: most text elements come preformated containing html tags like <p>, <br>, <ul> etc. As an indicator for these tags, the texts are wrapped inside a CDATA tag as follows:

json:

{
    "offerName": "<![CDATA[<b>Testoffer 1</b>]]>"
}

angularjs:

$http.get(url).success(function(data) {
    $scope.data = data;
});

html:

<h1>{{data.offerName}}</h1>

Now, how could I let the clientside remove any CDATA tags before they are shown?

1
  • 1
    Something like .replace('<![CDATA[', '').replace(']]', ''); or something CDDATA/angular/json specific? Commented Aug 7, 2015 at 14:26

1 Answer 1

4

You can use this regular expression:

'<![CDATA[<b>Testoffer]]> 1</b>]]>'.replace(/^<\!\[CDATA\[|\]\]>$/g,'')

It only matches the the CDATA if they are at the start and end of the string. Remove the ^ and $ to match anywhere

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.