I'm building a contenteditable editor which involves multiple rows of contenteditable divs
I want to turn this:
Lorem <b>Ipsum</b><b> Dolor</b> Sit Amet
into this:
Lorem <b>Ipsum Dolor</b> Sit Amet
I'm building a contenteditable editor which involves multiple rows of contenteditable divs
I want to turn this:
Lorem <b>Ipsum</b><b> Dolor</b> Sit Amet
into this:
Lorem <b>Ipsum Dolor</b> Sit Amet
For further actions, you may consider blocking this person and/or reporting abuse
arjun -
Crypto.Andy (DEV) -
Mohamed Ibrahim -
Lakshmi Sankar -
Top comments (8)
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.
But parsing html with regex is fun...
Not as much as having dinner at home. ;)
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.