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?
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:
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]);
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With