11

How to remove //<![CDATA[ and end //]]> with javascript from string?

var title = "<![CDATA[A Survey of Applications of Identity-Based Cryptography in Mobile Ad-Hoc Networks]]>" ;

needs to become

var title = "A Survey of Applications of Identity-Based Cryptography in Mobile Ad-Hoc Networks";

How to do that?

Vince
  • 3,274
  • 2
  • 26
  • 28
Alireza
  • 5,444
  • 9
  • 38
  • 50

5 Answers5

27

You can use the String.prototype.replace method, like:

title = title.replace("<![CDATA[", "").replace("]]>", "");

This will replace each target substring with nothing. Note that this will only replace the first occurrence of each, and would require a regular expression if you want to remove all matches.

Reference:

Ian
  • 50,146
  • 13
  • 101
  • 111
  • How will it work if your string has multiple `<![CDATA[` ? – Aamir Afridi Apr 11 '18 at 15:28
  • This trivial answer is wrong, because you can encode the literal string "<![CDATA[" inside a <![CDATA[ structure itself - the only way to properly "remove" the *outside* <![CDATA[ string is to remove the wrappers one-at-a-time from left to right, taking care NOT to remove any **encoded** "<![CDATA[" constructs that are meant to be inside there... Specifically - the thing you want to DECODE originally had THIS done to it in order to first ENCODE it: .replaceAll(']]>',']]]]><![CDATA[>'); –  Aug 30 '21 at 06:43
1

You ought to be able to do this with a regex. Maybe something like this?:

var myString = "<![CDATA[A Survey of Applications of Identity-Based Cryptography in Mobile Ad-Hoc Networks]]>";
var myRegexp = /<!\[CDATA\[(.*)]]>/;
var match = myRegexp.exec(myString);
alert(match[1]);
Curtis
  • 3,931
  • 1
  • 19
  • 26
1

I suggest this wider way to remove leading and trailing CDATA stuff :

title.trim().replace(/^(\/\/\s*)?<!\[CDATA\[|(\/\/\s*)?\]\]>$/g, '')

It will also work if CDATA header and footer are commented.

Vince
  • 3,274
  • 2
  • 26
  • 28
0

You must perform the OPPOSITE of what was originally done to the string to ENCODE it, which was this:-

mystr='<!CDATA[' + mystr.replaceAll(']]>',']]]]><![CDATA[>') + ']]>'

All the other answers on this page that suggest "replace" without using a loop are wrong.

  • FYI: CDATA encoding requires that replaceAll step to "escape" any potential "]]>" sequence that might be inside the string you want to encode. –  Aug 30 '21 at 06:50
0

The regular expresion /^(<!\[CDATA\[)|(]]>)$/gm worked for me without looping.