First turn your HTML into XHTML, then do any modifications you want with an XML tool.
If you have an input like this:
$ cat foo
<div class="elementOne">..</div>
<div class="elementTwo">
<div class="SubelementOne">....</div>
<div class="SubelementTwo">....</div>
</div>
Clean it up into valid XHTML with tidy
and then easily move elements with class SubelementTwo
under an element with class elementOne
:
$ tidy -asxhtml foo 2>/dev/null | \
xmlstarlet ed -m '//*[@class="SubelementTwo"]' '//*[@class="elementOne"]'
<?xml version="1.0"?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta name="generator" content="HTML Tidy for HTML5 for Linux version 5.2.0"/>
<title/>
</head>
<body>
<div class="elementOne">..<div class="SubelementTwo">....</div></div>
<div class="elementTwo">
<div class="SubelementOne">....</div>
</div>
</body>
</html>
This is more robust than regex based solutions.