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?

5 Answers 5

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:

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

2 Comments

How will it work if your string has multiple <![CDATA[ ?
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[>');
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]);

1 Comment

I would just get the array value in a single line: var match = myRegexp.exec(myString)[1];
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.

Comments

1

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

Comments

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.

1 Comment

FYI: CDATA encoding requires that replaceAll step to "escape" any potential "]]>" sequence that might be inside the string you want to encode.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.