Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 723483
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T06:06:48+00:00 2026-05-14T06:06:48+00:00

I have the following HTML <p>Some text <a title=link href=http://link.com/ target=_blank>my link</a> more text

  • 0

I have the following HTML

<p>Some text <a title="link" href="http://link.com/" target="_blank">my link</a> more 
text <a title="link" href="http://link.com/" target="_blank">more link</a>.</p>
<p>Another paragraph.</p>
<p>[code:cf]</p>
<p>&lt;cfset ArrFruits = ["Orange", "Apple", "Peach", "Blueberry", </p>
<p>"Blackberry", "Strawberry", "Grape", "Mango", </p>
<p>"Clementine", "Cherry", "Plum", "Guava", </p>
<p>"Cranberry"]&gt;</p>
<p>[/code]</p>
<p>Another line</p>
<p><img src="http://image.jpg" alt="Array" />
</p>
<p>More text</p>
<p>[code:cf]</p>
<p>&lt;table border="1"&gt;</p>
<p> &lt;cfoutput&gt;</p>
<p> &lt;cfloop array="#GroupsOf(ArrFruits, 5)#" index="arrFruitsIX"&gt;</p>
<p>  &lt;tr&gt;</p>
<p> &lt;cfloop array="#arrFruitsIX#" index="arrFruit"&gt;</p>
<p>     &lt;td&gt;#arrFruit#&lt;/td&gt;</p>
<p> &lt;/cfloop&gt;</p>
<p>  &lt;/tr&gt;</p>
<p> &lt;/cfloop&gt;</p>
<p> &lt;/cfoutput&gt;</p>
<p>&lt;/table&gt;</p>
<p>[/code]</p>
<p>With an output that looks like:</p>
<p><img src="another_image.jpg" alt="" width="342" height="85" /></p>

What I’m trying to do, is write a regular expression that will remove all the <p> or </p>, and whenever it finds a </p>, it will replace it with a line-break.

So far, my pattern looks like this:

/\<p\>(.*?)(<\/p>)/g

And I’m replacing the matches with:

$1\n

It all looks good, but it’s also replacing the contents inside the [code][/code] tags, which in this case should not replace the <p> tags at all, so as a result, i would lkike to get rid of the <p> tags, when the content isn’t inside the [code] tags.

I can’t ever get negation right, I know it will be something along the lines of

\<p\>^\[code*\](.*?)(<\/p>)

But obviously this doesn’t work 🙂

Could anyone please lend me a hand with this regex?

BTW, I know I shouldn’t be using regular expressions to parse HTML at all. I’m fully aware of that, but still, for this specific case, I’d like to use regex.

Thanks in advance

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T06:06:48+00:00Added an answer on May 14, 2026 at 6:06 am

    I assume that you have special knowedge about the application which generated the HTML you are venturing to parse, otherwise you would not be even considering regular expressions for the task. (Part of that is also, I assume, knowledge that <p> tags always appear after a newline and that </p> closing tags always appear before a newline.)

    The above having been said, you cannot easily or efficiently achieve what you are trying to achieve with regular expressions alone (you would have to use complex nested look-behind and look-ahead assertions to validate that your <p>...</p> occurrence is not inside a [code]...[/code] block, and non-fixed-length look-behind assertions are particularly limited, and IIRC plain buggy prior to JDK1.6.)

    You should first iterate over the input sequence, breaking it down into code and non-code chunks, and transferring the chunks into the output sequence either unchanged (in the case of code chunks) or with <p>...</p>-substitution applied via regex or simple string replacement (in the case of non-code chunks.)

    Up to you if you will have to (or how you want to) deal with nested or mismatched code chunks.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following html <div class=someClass> Some text </div> <div class=someClass> Some other
I have the following html code: <mytag> Just Some Text </mytag> And I have
I have some html like the following: <div class=control-group> <input type=text data-bind=value: $data.DealCode name=DealCode
I have the following html: <!DOCTYPE html> <html> <head> <script src=http://code.jquery.com/jquery-latest.js></script> </head> <body> <span>not
I have the following site: http://webofdreams.ro/vworker/finemaid/finemaid.html There are some java script(jquery 172) issues with
I have the following file file: <html> <head> <title></title> <script type=text/javascript src=/Prototype.js></script> <script type=text/javascript>
Suppose I have the following html: This a test of <code>some code</code>. <div class='highlight'>
I have the following code in JavaScript and jQuery: $(<li />) .html('Some HTML') I
I have following Situation, Server A sends some data (HTML form) to server B,
I have the following code: <?php if ($x == 1){ ?> <b>Some html...</b> <?php

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.