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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T00:03:34+00:00 2026-05-23T00:03:34+00:00

console.log( html.match( /<a href=(.*?)>[^<]+<\/a>/g )); Instead of returning just the urls like: http://google, http://yahoo.com

  • 0
console.log( html.match( /<a href="(.*?)">[^<]+<\/a>/g ));

Instead of returning just the urls like:

http://google, http://yahoo.com

It’s returning the entire tag:

<a href="http://google.com">Google.com</a>, <a href="http://yahoo.com">Yahoo.com</a>

Why is that the case?

  • 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-23T00:03:35+00:00Added an answer on May 23, 2026 at 12:03 am

    You want RegExp#exec and a loop accessing the element at the match result’s 1 index, rather than String.match. String.match doesn’t return the capture groups when there’s a g flag, just an array of the elements at index 0 of each match, which is the whole matching string. (See Section 15.5.4.10 of the spec.)

    So in essence:

    var re, match, html;
    
    re = /<a href="(.*?)">[^<]+<\/a>/g;
    html = 'Testing <a href="http://yahoo.com">one two three</a> <a href="http://google.com">one two three</a> foo';
    
    re.lastIndex = 0; // Work around literal bug in some implementations
    for (match = re.exec(html); match; match = re.exec()) {
      display(match[1]);
    }
    

    Live example

    But this is parsing HTML with regular expressions. Here There Be Dragons.


    Update re dragons, here’s a quick list of things that will defeat this regexp, off the top of my head:

    1. Anything other than exactly one space between the a and href, such as two spaces rather than one, a line break, class='foo', etc., etc.
    2. Using single quotes rather than double quotes around the href attribute.
    3. Not using quotes around the href attribute at all.
    4. Anything after the href attribute that also uses double quotes, e.g.:

      <a href="http://google.com" class="foo">
      

    This is not to be down on your regexp, it’s just to highlight that regular expressions can’t be reliably used on their own to parse HTML. They can form part of the solution, helping you scan for tokens, but they can’t reliably do the whole job.

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

Sidebar

Related Questions

Take a look at the example here: http://www.brianhare.com/physics/so.html Take a look at console.log where
$.getJSON('http://twitter.com/followers/ids.json?screen_name=' + query1 + '&callback=?', function (data) { console.log('JSON data string 1 is: '
I'm trying to use a raphael.js example located here: http://raphaeljs.com/pie.html but I want to
I was reading Crockford's tutorial http://javascript.crockford.com/private.html . private variables are implemented by function inside
Consider the following two alternatives: console.log("double"); console.log('single'); The former uses double quotes around the
I have a bunch of console.log() calls in my JavaScript. Should I comment them
this is my code and have a error: $('#map_canvas').mouseover(function(e){ console.log(e.offset().left+' '+e.offset().top) }) thanks i
$(item).droppable({ drop: function(event, ui) { console.log(triggered); } }); I try to call drop by
Firebug is certainly a wonderful tool for javascript debugging; I use console.log() extensively. I
Here is my code: var showNo = 1; window.setInterval(function() { console.log(showNo); if(showNo === 1)

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.