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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T06:13:34+00:00 2026-05-29T06:13:34+00:00

Let’s say I have the following text: (example) <table> <tr> <td> <span>col1</span> </td> <td>col2</td>

  • 0

Let’s say I have the following text: (example)

<table>
  <tr>
    <td>
      <span>col1</span>
    </td>
    <td>col2</td>
  </tr>
  <tr>
    <td>text1</td>
    <td>
      <span>text2</span>
    </td>
  </tr>
</table>

I want to replace all <span>%</span> by %, and I’ve come up with a solution like this:

replace(/<span>(.*)<\/span>/gi, function(full, text){return text;})

It replaces from the first span until the last one by only one occurrence, therefore the whole structure of my table is messed up.

How could I tell JS to replace each occurrence by the right one and not everything at once? The solution needs to be in Javascript obviously. I hope my example is not too “simple” and bugged to avoid any confusion.

  • 1 1 Answer
  • 1 View
  • 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-29T06:13:35+00:00Added an answer on May 29, 2026 at 6:13 am

    .* is greedy, so will happily match </span>...<span>. Replace it with [\s\S]*? which is non-greedy, but (unlike .) matches any character including newlines.

    /<span>([\s\S]*?)<\/span>/gi
    

    Better yet, parse it properly to a DOM and then change the spans there.

    EDIT:

    Rather than learn how to kind-of-parse HTML with regular expressions, your time would be better spent learning the DOM manipulation tools that are better suited to this problem.

    To parse the HTML, you can do

    var container = document.createElement('DIV');
    container.innerHTML = myStringOfHTML;
    

    Then

    container.getElementsByTagName('SPAN')
    

    will get all the SPANs.

    Finding the ones that contain only a text node is simple:

    var spans = container.getElementsByTagName('SPAN');
    for (var i = 0, n = spans.length; i < n; ++i) {
      var span = spans[0];
      // do work here
    }
    

    to fold the children into the parent,

    var spans = document.getElementsByTagName('SPAN');
    for (var i = 0, n = spans.length; i < n; ++i) {
      var span = spans[0];
      while (span.firstChild) {
        span.parentNode.insertBefore(span, span.firstChild);
      }
      span.parentNode.removeChild(span);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I have a text file composed like this ##### typeofthread1 ##### typeofthread2
Let's say I have the following object: var VariableName = { firstProperty: 1, secondProperty:
Let's say I have the following classes : public class MyProductCode { private String
Let say I've this URL: http://example.com/image-title/987654/ I want to insert download to the part
Let's say I have the string: hello world; some random text; foo; How could
Let's say I have table with column 'URL' whrere I store urls like this
Let say I have some code HTML code: <ul> <li> <h1>Title 1</h1> <p>Text 1</p>
Let's say I have the following function in C#: void ProcessResults() { using (FormProgress
Let say I have the following desire, to simplify the IConvertible's to allow me
Let's say you have a class called Customer, which contains the following fields: UserName

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.