Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

remove break tags from string using regex

Tags:

regex

sql

This works great removing ALL html from a string/DB text type field, how can I omit break tags:

update hazHRA set identityRisk=dbo.RegexReplace('<(?:[^>''"]*|([''"]).*?\1)*>',
'',identityRisk,1,1);

I wish to keep the

<br> 

only

like image 863
Mat41 Avatar asked Sep 20 '26 08:09

Mat41


1 Answers

This should do the job:

(?i)<(?:(?!br>|br/>)[^>'"]*|(['"]).*?\1)*>

(?i): Case insensitive.

(?!br>|br/>): Negative lookahead.

Online demo.


If you could use quantifiers in lookaheads you may use this:

(?i)<(?:(?!br\s*>|br\s*/>)[^>'"]*|(['"]).*?\1)*>

This will ensure to not match <br > with spaces.

Online demo.

like image 124
HamZa Avatar answered Sep 22 '26 21:09

HamZa



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!