Home > Blockchain >  How to replace a variable number of instances in a string?
How to replace a variable number of instances in a string?

Time:10-14

A front end WYSIWYG editor in our application has been producing strings with gobs of <p> tags in the editable string. Every time the records were updated, the <p> tags would double. How can I fix the data, reducing n number of consecutive <p> tags in the string with a single one?

Change this:

<p><p>Hello, world

or this:

<p><p><p><p><p><p><p><p><p>Hello, world

to this:

<p>Hello, world

CodePudding user response:

You can do it with using regular expression. Example:

select regexp_replace('<p><p><p><p><p><p><p><p><p>Hello, world', '(<p>)\1{1,}', '\1', 'g');

Result: 
<p>Hello, world
  • Related