The result of my innerHTML is like below in javascript.
"<ol><li>Testing1</li><li>Testing2</li></ol>"
Can I align it in a code format as below using regex or anyother? any suggestion on this ?
<ol>
<li>Testing1</li>
<li>Testing2</li>
</ol>
The result of my innerHTML is like below in javascript.
"<ol><li>Testing1</li><li>Testing2</li></ol>"
Can I align it in a code format as below using regex or anyother? any suggestion on this ?
<ol>
<li>Testing1</li>
<li>Testing2</li>
</ol>
The problem isn't trivial. One issue with regular expressions is that Javascript's can't handle nested matching delimiters in most cases. Another issue is that you need to keep track of self-closing tags (<div />
, or tags which don't have closing tags (<img>
, <input>
) and programmatically identify whether the text you're currently processing is part of structured HTML markup, or whether it's just plain text (like <textarea>foo<div>bar</div></textarea>
).
It would probably be easier to use a library designed for this purpose, so as not to re-invent the wheel. js-beautify is one option:
const input = "<ol><li>Testing1</li><li>Testing2</li></ol>";
console.log(html_beautify(input, { 'indent-size': 2 }));
<script src="https://cdnjs.cloudflare.com/ajax/libs/js-beautify/1.10.2/beautify-html.js"></script>