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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T22:29:58+00:00 2026-05-17T22:29:58+00:00

I am getting different results from regex match when using both regular JS and

  • 0

I am getting different results from regex match when using both regular JS and jQuery v1.4.2 and unable to figure out why. Only match string should be returned.
I use jQuery to grab the whole table via the parent ID using .html(). textToSearch is shorten.

textToSearch = '<tr><th colspan="5">my match with spaces here (<a href=';
pattern = /(?=<th colspan="\d">).*(?= \()/i;
expected_result = 'my match with spaces here';

var match = textToSearch.match(pattern);

In regular JS I get expected result, but in jQuery I get ‘my match with spaces here’.
Am I doing something wrong is jQuery messing things up ?
Maybe there is a better way getting the expected result ?

Edit: Solution below.

var pattern = /.*(?= \()/;
var t = $('#'+id+' th[colspan]').text();
$('#'+targetid).text(t.match(pattern)[0]);
  • 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-17T22:29:59+00:00Added an answer on May 17, 2026 at 10:29 pm

    jQuery’s html() will not return the html in the format you’re expecting above, and it may even differ between browsers. The string returned is constructed from the browser’s representation of the DOM tree, and so will look nothing like the “view source” feature of most browsers. Parsing HTML with regular expressions isn’t a great idea for this reason (and others).

    It’s unclear to me why you’re using a regular expression. If you’re grabbing the html with html(), you may as well just filter the nodes and grab their text instead. It would be a much more robust solution. For instance, the following code may do the job for you:

    var result = $("table th[colspan]")[0].firstChild.nodeValue;
    

    Or perhaps you want the entire text for that <th> element?

    var result = $("table th[colspan]").text();
    

    Either way, there’s almost certainly a better way to attain the match you’re after without using regular expressions.

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

Sidebar

Related Questions

I am getting a set of data from the database where the query produces
I am trying to get the source page of a webpage on a different
I need to get summary data from many many rows. The summary fields are
We have a simple PHP site. It is getting hit quite a bit. We
I have two queries that I thought meant the same thing, but I keep
I need to execute stored procedure sp_spaceused for all the tables in my database.
I'm adding networked multiplayer to a game I've made. When the server sends an
In drupal 6 I'm trying to execute a function on every page and output
I am running 2 queries against an Informix database. The queries are hitting 4
Sorry I missed and deleted earlier question on accident again. I have a situation,

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.