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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T08:56:18+00:00 2026-05-11T08:56:18+00:00

Given a string like this: <a href=http://blah.com/foo/blah>This is the foo link</a> … and a

  • 0

Given a string like this:

<a href='http://blah.com/foo/blah'>This is the foo link</a> 

… and a search string like ‘foo’, I would like to highlight all occurrences of ‘foo’ in the text of the HTML — but not inside a tag. In other words, I want to get this:

<a href='http://blah.com/foo/blah'>This is the <b>foo</b> link</a> 

However, a simple search-and-replace won’t work, because it will match part of the URL in the <a> tag’s href.

So, to express the above in the form of a question: How do I restrict a regex so that it only matches text outside of HTML tags?

Note: I promise that the HTML in question will never be anything pathological like:

<img title='Haha! Here are some angle brackets to screw you up: ><' /> 

Edit: Yes, of course I’m aware that there are complex libraries in CPAN that can parse even the most heinous HTML, and thus alleviate the need for such a regex. On many occasions, that’s what I would use. However, this is not one of those occasions, since keeping this script short and simple, without external dependencies, is important. I just want a one-line regex.

Edit 2: Again, I know that Template::Refine::Fragment can parse all my HTML for me. If I were writing an application I would certainly use a solution like that. But this isn’t an application. It’s barely more than a shell script. It’s a piece of disposable code. Being a single, self-contained file that can be passed around is of great value in this case. ‘Hey, run this program’ is a much simpler instruction than, ‘Hey, install a Perl module and then run this– wait, what, you’ve never used CPAN before? Okay, run perl -MCPAN -e shell (preferably as root) and then it’s going to ask you a bunch of questions, but you don’t really need to answer them. No, don’t be afraid, this isn’t going to break anything. Look, you don’t need to answer every question carefully — just hit enter over and over. No, I promise, it’s not going to break anything.’

Now multiply the above across a great deal of users who are wondering why the simple script they’ve been using isn’t so simple anymore, when all that’s changed is to make the search term boldface.

So while Template::Refine::Fragment may be the answer to someone else’s HTML parsing question, it’s not the answer to this question. I just want a regular expression that works on the very limited subset of HTML that the script will actually be asked to parse.

  • 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. 2026-05-11T08:56:18+00:00Added an answer on May 11, 2026 at 8:56 am

    If you can absolutely guarantee that there are no angle brackets in the HTML other than those used to open and close tags, this should work:

    s%(>|\G)([^<]*?)($key)%$1$2<b>$3</b>%g 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 182k
  • Answers 182k
  • 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 have to invoke the command from the folder you… May 12, 2026 at 4:28 pm
  • Editorial Team
    Editorial Team added an answer This probably belongs on doctype, but I think you want… May 12, 2026 at 4:28 pm
  • Editorial Team
    Editorial Team added an answer See my answer to Manually change the ClientBase collection type… May 12, 2026 at 4:28 pm

Related Questions

Given a string like this: <a href=http://blah.com/foo/blah>This is the foo link</a> ... and a
Trying to parse an HTML document and extract some elements (any links to text
I'm trying to substitute something in a string in python and am having some
I've retrieved a series of objects from the Twitter API, and I want to
I have a little problem that perhaps you can help me with. I try

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.