views:

79

answers:

2

Hello, I'm trying to remove the CDATA wrapper in the following script (content has to be wrapped by CDATA to pass XHTML validation):

<script id="tplTest" type="text/html">

//<![CDATA[ 
<p id="msg">Hello</p>
<p>Another test: <#= ddd.ArtID #></p> 
//]]>

</script>

This js script:

var strTmp=document.getElementById("tplTest").innerHTML;
var strNew= strTmp.replace(/[\/(\/!\[)\]CDATA]/g,"").replace(/[(\/\/\]\])]/g,"");

removes most to the CDATA mark-up except for the start/end<,> tags:

< 
<p id="msg">Hello<p>
<p>nother test: <#= ddd.rtI #><p> 
>

Question: How should I modify the js function to additionally remove these leading and trailing <,> tags? TIA for any hints.

+5  A: 

You could just replace the raw string and skip using regular expressions all-together:

"FOO BAR".replace("FOO", ""); // replace "FOO" with "" (nothing)

In your case:

var stringToSanitize = "//<![CDATA[ xxx //]]>";

var sanitizedString = stringToSanitize
                      .replace("//<![CDATA[", "")
                      .replace("//]]>", "");

Regular expressions in JavaScript are slow. So on top of you getting your problem solved, you might see a slight speed-increase using my example.

roosteronacid
A: 

Isn't it enough to just add a < after the first slash in the first replace and a '>' after the last slash in the last replace? If your regex dialect takes these angle brackets as magic characters (few do) you can use \< and \> respectively, i.e., escape them with backslashes.

Alex Martelli