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

Alireza picture Alireza · Jun 7, 2012 · Viewed 19.4k times · Source

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?

Answer

Ian picture Ian · Jun 7, 2012

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: