Skip to content
markdown guide

Using Regex to parse and manipulate HTML is a recipe for disaster. There are too many edge cases you have to account for.

If you really want to "optimize" the HTML, you may end up having to parse HTML into some kind of AST and work from there. But this is probably more complex than the editor itself you want to build.


Simply remove any occurence of "</b><b>":

"Lorem <b>Ipsum</b><b> Dolor</b> Sit Amet".replace("</b><b>", "")


Would be good to do this in a regex that also looks for whitespace between the two tags.


I thought about that as well, but then you'd change the content (if only ever so slightly) for a trivial QoL improvement

If you use a capture group, you could put every bit of whitepace back.


Regardless of the Element, are you always going to see two elements together? More importantly what is the implications of not doing as you propose. If you can do no work that's counterintuitively better than fixing something that might not need a fix.

Classic DEV Post from Nov 7 '19

What Are the Most Important CS Principles to Learn as a New Dev from a Non-Traditional Background?

Arnav Bansal profile image
I slay dragonsβ€”or take them as pets.

Be a better developer. Free forever.