Having an issue with a JavaScript regex that needs to comment out all <script> tags inside a <script> tag except the first one with the id "ignorescript".
Here is a sample string to work with:
<script id="ignorescript">
var test = '<script>test<\/script>;
var xxxx = 'x';
</script>
The script tag inside ignorescipt has an extra backslash because it is JSON encoded (from PHP).
Here is the desired final result:
<script id="ignorescript">
var test = '<!ignore-- <script>test<\/script> ignore-->;
var xxxx = 'x';
</script>
The following example is functional:
content = content.replace(/(<script>.*<\\\/script>)/g,
"<!--ignore $1 ignore-->");
However, I need to ensure it does not contain the keyword "ignorescript". If that keyword is present, no replacement should occur. Otherwise, add ignore comments to the entire script tag. So far, the regex looks like this:
content = content.replace(/(<script.((?!ignorescript).)*<\\\/script>)/g,
"<!--ignore $1 ignore-->");
It somewhat works, but not as expected. There is also an issue with the ending tag backslash, so I modified it to this:
content = content.replace(/(<script.((?!ignorescript).)*<\\\/script>)/g,
"<!--ignore $1 ignore-->");
Now it does not find anything at all.