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 !

3
  • 1
    d.*t p.*s.*e h.*l w.*h r.*g.*x Commented Dec 16, 2015 at 6: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? Commented Dec 16, 2015 at 6:11
  • @Tushar Okay - but what library supports a replace operation of any kind of tag to a specified tag? Commented Dec 16, 2015 at 6:30

1 Answer 1

1

You can use this:

aaaaaaaaaaa%0D%0A%0D%0A+%0D%0A%0D%0A
%0D%0A
&r=<${close}p>" rel="nofollow"><(?<close>/?)((?!img|br).)*?>

and replace with:

<${close}p>

CODE SAMPLE

Sign up to request clarification or add additional context in comments.

1 Comment

It has a more character '?' in the end of <${close}p>?

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.