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 345491
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T11:05:20+00:00 2026-05-12T11:05:20+00:00

I have a need to perform search and replace operations on large blocks of

  • 0

I have a need to perform search and replace operations on large blocks of HTML. I do not wish to change anything that is part of an html tag (like urls) – I also do not wish to change urls OUTSIDE of html tags. I have a partial solution for matching a word that is not inside of html (src):

 word(?!([^<]+)?>)

while regex buddy also says that this will match the same:

 (?!([^<]+)?>)word

so, the only thing left to do is ensure that word is not part of a string that looks like a url – like this:

(https?|ftp|file)://[-A-Z0-9+&@#/%?=~_|$!:,.;]*[A-Z0-9+&@#/%=~_|$]

I am unsure if this is possible, my intention is to preserve urls that are present in the text, and are part of the html of the content, while allowing search and replace operations on anything else:

The ideal solution would match DOG and replace with CAT as illustrated below

<h1>DOG</h1> -> <h1>CAT</h1>
<h1 class='DOG'>DOG</h1> -> <h1 class='DOG'>CAT</h1>

<p class='DOG'>DOG: http://www.DOG.com/DOGfood.html DOGfood is delicious.</p> -> <p class='DOG'>CAT: http://www.DOG.com/DOGfood.html CATfood is delicious.</p>

Bonus points for efficiency, I am nearly at my wits end.

  • 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-12T11:05:20+00:00Added an answer on May 12, 2026 at 11:05 am

    As for matching “DOG” not in a tag: that’s how I would do it in general, but I would use this regex instead:

    DOG(?![^<>]++>)
    

    [^<>]++ matches one or more of anything that’s not an angle bracket possessively. Once it’s done, if the next thing isn’t '>' it reports failure immediately–no backtracking. You can’t get more efficient than that.

    However, your idea of using a lookbehind to determine whether you’re inside a URL won’t work. That would require a variable-length lookbehind match, and PHP doesn’t support that–very few regex flavors do.

    I recommend an alternation-based approach instead. In a single regex, you match either a complete HTML tag, a complete URL, or your word:

    <[^<>]++>
    |
    (https?|ftp|file)://[A-Z0-9+&@#/%?=~_|$]++(?:[?!:,.;-]++[A-Z0-9+&@#/%=~_|$]++)*+
    |
    DOG
    

    Use preg_replace_callback to apply the regex, and in the callback you check what it matched. If it’s a tag or a URL, plug it back in; if it’s “DOG” you replace it with “CAT”.

    This assumes every angle bracket in the file is part of an HTML tag. If your files may contain SGML comments, you’ll have to add an alternative for them, before the one for HTML tags. The same goes for CDATA sections. And of course, attribute values are permitted to contain angle brackets too. That’s extremely rare in my experience, but it can be handled too, if necessary.

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

Sidebar

Related Questions

I have a task I need to perform, do_stuff(opts) , that will take ~1s
I have a class that I need to perform some actions on but I
I have a long-running cleanup operation that I need to perform in onDestroy() of
I have a lotus view that stores a number. I need to perform some
I'm trying to perform a search-replace in several JCLs but I need multi-line capabilities,
I need to perform a search/replace on text which contains a comma which is
I have a simple search form that looks something like: app/views/search/index.html.erb <%= form_for @search,
I have a spreadsheet with many names, I need to perform google search for
I have a form that lets a user perform a search for a stock
I have the need to perform algorithms on various primitive types; the algorithm is

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.