sql server - TSQL remove img tag with specific src from HTML -


i have html text in database many img tags. goal remove img tags specific src

my input is

<div>     <p>some text goes here <img width="100" src="/upload/remove-me.png" /></p>     <p>some other text goes here <img height="100" src='/upload/remove-me.png' width="200" /></p>         <p>some other text goes here <img src="/upload/filename.png" /></p> </div> 

i'd remove images src="/upload/remove-me.png" output result be

<div>     <p>some text goes here</p>     <p>some other text goes here</p>     <p>some other text goes here <img src="/upload/filename.png" /></p> </div> 

is there way regex in tsql?

from example seems tags can have attributes in order, need loop through text take out img tags 1 @ time. want try on backed version of data make sure removing want removed:

declare @html table(a nvarchar(max))  insert @html select  '<div>     <p>some text goes here <img width="100" src="/upload/remove-me.png" /></p>     <p>some other text goes here <img height="100" src="/upload/remove-me.png" width="200" /></p>         <p>some other text goes here <img src="/upload/filename.png" /></p> </div>'   declare @url nvarchar(50) = 'src="/upload/remove-me.png"'   -- search img tags text in. declare @tagstart int = -1 declare @tagend int = -1  while @tagstart <> 0 begin     select @tagstart = patindex('%<img%' + @url + '%/>%',a)-1       -- find start of first img tag in text.             ,@tagend = patindex('%/>%'                                         ,substring(a                                         ,patindex('%<img%' + @url + '%/>%',a)                                         ,999999999                                         )                                 )+1                                 -- find end of first img tag in text.     @html      update @html                -- update table remove tag     set = (select left(a,@tagstart) + right(a,len(a)-@tagstart-@tagend)             @html             )      select @tagstart = patindex('%<img%' + @url + '%/>%',a)     -- check if there more img tags url remove.  return 0 if there none.     @html end  select cleanhtml @html 

Comments