1

We having a HTML content like

<em></ em > 
<font style="text-align:justify;">aaaaaaaaaaa</font>
<img src="abc.jpg"/>
<iframe src="somelink.com"> 
</iframe>
<br>
<br/>

We want to change all HTML Tags to <p></p>

but do not change the <img/> and <br/> tag, some <br/> tags may display <br>

so, the following is our expected result:

<p></p> 
<p>aaaaaaaaaaa</p>
<img src="abc.jpg"/>
<p> 
</p>
<br>
<br/>

My regular expression like (in C#):

String result = Regex.Replace(content, @"<[^/b>]*>", "<p>");
result = Regex.Replace(result, @"</[^>]*>", "</p>");  

but it can't skip the certain tags, please help me, thanks !

Steven Chou
  • 1,504
  • 2
  • 21
  • 43
  • 1
    `d.*t p.*s.*e h.*l w.*h r.*g.*x` – Avinash Raj Dec 16 '15 at 06:11
  • 1
    Don't use regex for HTML changes, use jQuery, or some similar HTML parsing library/functions. [Using regular expressions to parse HTML: why not?](http://stackoverflow.com/questions/590747/using-regular-expressions-to-parse-html-why-not) – Tushar Dec 16 '15 at 06:11
  • @Tushar Okay - but what library supports a replace operation of any kind of tag to a specified tag? – Sebastian Schumann Dec 16 '15 at 06:30

1 Answers1

1

You can use this:

<(?<close>/?)((?!img|br).)*?>

and replace with:

<${close}p>

CODE SAMPLE

Sebastian Schumann
  • 3,204
  • 19
  • 37