I have a string of HTML, in this example it looks like
<img src="somepic.jpg" someAtrib="1" >
I am trying to workout a peice of regex that will match the 'img' node and apply a slash to the end of the node so it looks like.
<img src="somepic.jpg" someAtrib="1" />
Essentially the end goal here is to ensure that the node is closed, open nodes are valid in HTML but not XML obviously. Are there any regex buff's out there able to help?
Don't use a Regular expression, but dedicated parsers. In JavaScript, create a document using the DOMParser
, then serialize it using the XMLSerializer
:
var doc = new DOMParser().parseFromString('<img src="foo">', 'text/html');
var result = new XMLSerializer().serializeToString(doc);
// result:
// <html xmlns="http://www.w3.org/1999/xhtml"><head></head><body> (no line break)
// <img src="foo" /></body></html>
You have to use xmldom if you required to use this with nodejs backend. npm i xmldom
.