Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Strip <?xml and <!DOCTYPE from string

I have the following string in Javascript and need to remove the <?xml ... ?> and <!DOCTYPE .... ]> tags. Can not convert it to a dom because the BR tags error as not being closed - and not able to edit the actual content.

  <?xml version="1.0" encoding="UTF-8"?><!DOCTYPE html [<!ENTITY amp "&#38;#38;">]><div>Blah<br> Blah</div>

Trying to do it with .replace but can't quite seem to get there

    text.replace(/\<\?xml.+\?\>/g, '');
like image 999
Louis W Avatar asked Dec 11 '22 16:12

Louis W


1 Answers

Your replace() works for the <?xml ... ?> part.

To remove the <!DOCTYPE .... ]> part as well you can do:

text.replace(/\<\?xml.+\?\>|\<\!DOCTYPE.+]\>/g, '');

As you can see here: http://jsfiddle.net/darkajax/9fKnd/1/

like image 82
DarkAjax Avatar answered Jan 05 '23 05:01

DarkAjax