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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T17:02:41+00:00 2026-05-26T17:02:41+00:00

I am trying to match all <tr> elements that contain the word Source, but

  • 0

I am trying to match all <tr> elements that contain the word “Source”, but when the other attributes (colspan/width/height, contained <td>s and their attributes, etc.) are unknown. (I know this can be done with a javascript/jQuery selector, but I am just processing the HTML for a non-javascript context.)

Example of target:

<tr>
<td>Don't affect this</td>
</tr>
<tr>
<td colspan="3" width="288" height="57"><strong>Sources:</strong> Author</td>
</tr>

(This is what I want to change it to:)

<tr>
<td>Don't affect this</td>
</tr>
<tr class="source">
<td colspan="3" width="288" height="57"><strong>Sources:</strong> Author</td>
</tr>

Here are regex patterns I have tried that haven’t worked:

 /<tr>((?:.*?)Source(?:s?):(?:.*?))<\/tr>/gmi,

No matches.

 /<tr>((?:[\s\S]*?)Source(?:s?):(?:[\s\S]*?))<\/tr>/gmi, 

Matches the first tr, but not the second.

I think there’s regex principle I may be failing to grasp here, about greediness or something related. Any suggestions?

  • 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-26T17:02:42+00:00Added an answer on May 26, 2026 at 5:02 pm
    /<tr[^>]*>(?:(?!<|source)[\s\S])*(?:<(?!\/?tr)[^>]*>(?:(?!<|source)[\s\S])*)*source[\s\S]*?<\/tr>/i
    

    Are you sure you can’t use jQuery for this? 😛 But seriously, this will be easier to grasp if I put it in terms of Friedl’s “unrolled loop” idiom:

    opening normal ( special normal * ) * closing
    
    • opening: <tr[^>]*> – the opening <tr> tag

    • normal: (?:(?!<|source)[\s\S])* – zero or more of any characters, with the lookahead to make sure each time that the character is not the beginning of a tag or the word “source”

    • special: <(?!\/?tr)[^>]*> – any tag except another opening <tr> or a closing </tr>. By consuming a complete tag, we avoid false positives on the word “source” in the name or value of an attribute.

    • closing: source – The only other thing it could possibly encounter here is a <tr> or </tr> tag, which would indicate a failed match for our purposes. Finding “source” before one of those tags is how we know we’ve found a match. (The rest of the regex, [\s\S]*?<\/tr>, merely consumes the remainder of the tag so you can retrieve it via group[0].)

    A <tr> there isn’t necessarily invalid, of course; it could be the beginning of a nested TR element, presumably within a nested TABLE element. If that TR contains the word “source”, the regex will match it on a separate match attempt. It will match only the innermost, complete TR tag with the word “source” in it.

    As usual when using regexes on HTML, I’m making several simplifying assumptions involving well-formedness, SGML comments, CDATA sections, etc., etc. Caveat emptor.

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

Sidebar

Related Questions

I'm trying to match elements with a name that is 'container1$container2$chkChecked' , using a
I'm trying to come up with a selector that will select all textarea elements,
I'm trying to do a preg match all on the response below to get
Given the HTML below, I am trying to use jQuery to match all the
I'm trying to find a jQuery selector that will match only the element that
I'm trying to get an XPath expression together that will give me all the
I'm trying to write a regular expression to match a string that may or
I have this XSLT stylesheet, where I'm trying to find nodes that contain an
I want to remove the class attribute from all elements that have an empty
I have a form and I want to disable all the Elements that do

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.