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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T04:27:55+00:00 2026-05-27T04:27:55+00:00

I hope this isn’t a repetition… I need a regex to do what should

  • 0

I hope this isn’t a repetition…

I need a regex to do what should be a fairly simple task. I have code for an HTML table, and I want to replace all <td> tags with <th> tags in the first row of the table, i.e. within the first set of <tr> </tr> tags. The table might look something like this:

<table cellpadding="5" cellspacing="0" border="1">
<tr>
<td>Capacity %</td>
<td>Tension V</td>
<td>Acid kg/l</td>
</tr>
<tr>
<td>100</td>
<td>12.70</td>
<td>1.265</td>
</tr>...etc

and I want:

<table cellpadding="5" cellspacing="0" border="1">
<tr>
<th>Capacity %</th>
<th>Tension V</th>
<th>Acid kg/l</th>
</tr>
<tr>
<td>100</td>
<td>12.70</td>
<td>1.265</td>
</tr>...etc

I’ve tried regexes similar to this:

/(<table>\n<tr>\n)(.+?)(</tr>)

…and then tried to rebuild the table row using back references, but I can’t seem to apply the regex to the multiple
</?td>
matches that there might be.

I’m doing this in javascript, which means I can’t use look-behinds (although if anyone has a look behind solution I’d be interested in seeing it anyway…).

Thanks in advance for any help.

  • 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-27T04:27:56+00:00Added an answer on May 27, 2026 at 4:27 am

    You could do it if your regex engine supports indefinite repetition inside lookbehind assertions, for example in .NET (C#):

    resultString = Regex.Replace(subjectString, 
        @"(?<=      # Assert that we can match this before the current position:
         <table     # <table
         (?:        # followed by...
          (?!       # (unless there's an intervening
           </table  #  </table
          |         #  or
           </tr     #  </tr)
          )         # (End of lookahead assertion)
          .         # any character
         )*         # any number of times
        )           # (End of lookbehind assertion)
        <td         # Then match <td", 
        "<th", RegexOptions.Singleline | RegexOptions.IgnorePatternWhitespace);
    

    works on your example. But even in .NET, I wouldn’t use a regex for it, it’s just too brittle. Better manipulate the DOM directly, that’s what it’s there for.

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

Sidebar

Related Questions

I hope this question isn't too dumb. We have many websites internally that need
I hope this isn't too simple, but I have a report retrieves rows using
I hope this isn't too difficult, obviously you can call HTML tags such as
I'm fairly new to servlets so I hope this isn't an obvious question. So
OK I hope this isn't too specific. I have a database driven CMS that
I hope this isn't too specific. I have created an XML Schema which i
First question. Hope this isn't a repeat. I have an unordered list of thumbnails
I hope this isn't too off topic for this forum, but I have been
Hope this isn't a waste of your time. I'm working on a project, and
Alright, I hope this isn't too broad a question but my curiosity got the

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.