1

I´d like to change a HTML snippet stored in database.

Let's assume there is only one table called table with columns id and content. In content column are stored HTML snippets

It has this kind of format

<div class="header">etc etc </div>

<div class="content"> content </div>

<div class="footer"> footer </div>

(and possibly more complex html structure).

The goal is to replace the content div (so div with class content, there is guaranteed that there is just one div with this class identifier) from stored string with some text, for example "TEXT" using SQL on PostgreSQL.

So in this case the result would be

<div class="header">etc etc </div>

TEXT

<div class="footer"> footer </div>

Thank you

1 Answer 1

1

I'd use REGEX_REPLACE() for this:

demo:db<>fiddle

SELECT
    REGEXP_REPLACE(html, '<div class="content">.*?</div>', 'MY TEXT')
FROM t
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for reply, it seems to work. The additional slash is a mistake, I´ve fixed that. Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.