Trying to use regex_replace to pull specific content from content.post_body

A client imported their blog in such a way that all the html, scripts, etc. are in the text editor for blog content. :person_shrugging: It looks fine on the imported blogs, but they’d need to know html to replicate it on any new blogs.

The new blog template will require custom modules to replicate layout, so rather than going through all the existing posts and manually copying over the data to the modules, I’d like to set placeholders that pull from the content.post_body initially.

So far I’ve done this:

{% module_block module “module_0000000” path=“/theme/_modules/my_module” %} {% module_attribute “text” %} <!-- START --> {{ content.post_body }} <!-- END --> {% end_module_attribute %} {% end_module_block %}

Which pulled in all the imported html as the content of the text attribute of “my_module”.
But I want to try to use regex_replace to remove content before and after a h6 for example to pull a custom mane that has been enter in the html. So:

< head ></ head > < body > < main > Lorem ipsum dolor sit amet, consectetur adipiscing elit. Cras eu rutrum enim. Vivamus id ex pharetra, posuere massa nec, hendrerit lacus. Pellentesque pellentesque, dui sed fermentum iaculis, ligula libero bibendum mi, a tincidunt mauris tellus sed nibh. Phasellus ac magna ac urna feugiat tincidunt ac nec arcu. Nullam lobortis ipsum ut dolor ornare, quis eleifend nisi sollicitudin. Cras egestas, ante vitae finibus rutrum, libero tellus finibus metus, nec rhoncus ipsum lacus at metus. Donec non ipsum vitae ante tempus semper. Sed sed ultrices, eu cursus tortor. </ main > < aside > <h6>I'm a heading</h6> </ aside > </ body >

I want the opposite of:

{{ content.post_body|regex_replace('<h6>(.*?)</h6>','') }}

Replace everything but h6 content

@BGrennan “<h6>” ~ content.post_body|split(“<h6>”)|last|split(“</h6>”)|first ~ “</h6>” … ?

That does the trick.
Could I also apply that to a paragraph with a class? Seems like the closing </p> might be trickier to find

Yes, and no. You can certainly split on anything you want and “divy” things up as I ahve with basic string ops. Don’t need a regex for this and using regexes for HTML is inherently wrong to start with for any number of technical reasons.

Your problem is going to be ordering - if multiple exist - and determinism + determinism of the definitions of the elements themselves. What you’re doing isn’t well-defined, but per your use case it’s a result of a poor migration. The real solution is to rebuild it and if the client doesn’t want to and/or can’t afford it… Drop them and hope for a better one. Because, this will just compound your problems eventually and I dislike giving this type of advice knowing what it will inevitably lead to.