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

  • Home
  • SEARCH
  • 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

Ask A Question

Stats

  • Questions 285k
  • Answers 285k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You could use a preg_match_all(...): $text = 'Lorem ipsum "dolor… May 13, 2026 at 4:41 pm
  • Editorial Team
    Editorial Team added an answer Your question is a bit vague, but check out the… May 13, 2026 at 4:41 pm
  • Editorial Team
    Editorial Team added an answer I used a lot from the comments people wrote and… May 13, 2026 at 4:41 pm

Related Questions

I have a properties file in which the keys represent strings in source code
Lately, I've come across a lot of Java code that relies on properties files
First post, so here goes. I'm writing a script that does intelligent search and
I need to write some code that performs an HTML highlight on specific keywords

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.