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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T18:51:00+00:00 2026-06-09T18:51:00+00:00

I can’t post the exact data i’m trying to extract but here’s a basic

  • 0

I can’t post the exact data i’m trying to extract but here’s a basic scenario with the same outcome. I’m grabbing the body of a page and trying to extract a bit.ly link from it. So let’s say for example, this is the chunk of data where i’m trying to grab the link from.

String:

<a href="/l.php?u=http%3A%2F%2Fbit.ly%2FPq8AkS&amp;h=aAQFZxdL0&amp;s=1" target="_blank"    rel="nofollow nofollow" onmouseover="LinkshimAsyncLink.swap(this, &quot;http:\\/\\/bit.ly\\/Pq8AkS&quot;);" onclick="LinkshimAsyncLink.referrer_log(this, &quot;http:\\/\\/bit.ly\\/Pq8AkS&quot;, &quot;http:\\/\\/www.facebook.com\\/si\\/ajax\\/l\\/render_linkshim_log\\/?u=http\\u00253A\\u00252F\\u00252Fbit.ly\\u00252FPq8AkS&amp;h=aAQFZxdL0&amp;s=1&quot;);">http://bit.ly/Pq8AkS</a></div><div class="shareUnit"><div class="-cx-PRIVATE-fbTimelineExternalShareUnit__wrapper"><div><div class="-cx-PRIVATE-fbTimelineExternalShareUnit__root -cx-PRIVATE-fbTimelineExternalShareUnit__hasImage"><a class="-cx-PRIVATE-fbTimelineExternalShareUnit__video -cx-PRIVATE-fbTimelineExternalShareUnit__image -cx-PRIVATE-fbTimelineExternalShareUnit__content" ajaxify="/ajax/flash/expand_inline.php?target_div=uikk85_59&amp;share_id=271663136271285&amp;max_width=403&amp;max_height=403&amp;context=timelineSingle" rel="async" href="#" onclick="CSS.addClass(this, &quot;-cx-PRIVATE-fbTimelineExternalShareUnit__loading&quot;);CSS.removeClass(this, &quot;-cx-PRIVATE-fbTimelineExternalShareUnit__video&quot;);"><i class="-cx-PRIVATE-fbTimelineExternalShareUnit__play"></i><img class="img" src="http://external.ak.fbcdn.net/safe_image.php?d=AQDoyY7_wjAyUtX2&amp;w=155&amp;h=114&amp;url=http%3A%2F%2Fi1.ytimg.com%2Fvi%2FDre21lBu2zU%2Fmqdefault.jpg" alt="" /></a>

Now, I can get what i’m looking for with the following code but the link isn’t always going to be exactly 6 characters long. So this causes an issue…

Body = document.getElementsByTagName("body")[0].innerHTML;
regex = /2Fbit.ly%2F(.{6})&amp;h/g;
Matches = regex.exec(Body);

Here’s what I was orginally trying but the problem I have is that it grabs too much data. It’s going all the way to the last "&amp;h" in the string above instead of stopping at the first one it hits.

Body = document.getElementsByTagName("body")[0].innerHTML;
regex = /2Fbit.ly%2F(.*)&amp;h/g;
Matches = regex.exec(Body);

So basically the main part of the string i’m trying to focus on is "%2Fbit.ly%2FPq8AkS&amp;h" so that I can get the “Pq8AkS” out of it. When I use the (.*) it’s grabbing everything between "%2F" and the very last "&amp;h" in the large string above.

  • 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-06-09T18:51:01+00:00Added an answer on June 9, 2026 at 6:51 pm

    You should not be using a regex on HTML. Use DOM functions to get the desired link object, then get the href attribute from that, then you can use a regex on just the href.

    By default .* is greedy meaning that it matches the most it can match and still find a match. If you want it to be non-greedy (match the least possible), you can use this .*? instead like this:

    regex = /2Fbit.ly%2F(.*?)&amp;h/;
    

    I also don’t think you want the g flag on the regex as there should only be one match in the right URL.

    If you show the rest of your HTML, we could offer advice on finding the right link object rather than trying to match the entire body HTML.


    FYI, another trick for a non-greedy match is to do something like this:

    regex = /2Fbit.ly%2F([^&]*)&amp;h/;
    

    Which matches a series of characters that are not & followed by &amp;h which accomplishes the same goal as long as & can’t be in the matched sequence.

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

Sidebar

Related Questions

Can we change the default action of the edit selected row button? Here is
Can anybody shed some light on getDay() in Javascript please. Here datepicker is textbox
Can someone help me with this scenario? *There is a button, which when tapped,
Can we put functions to determine the condition for our list comprehensions. Here is
Can someone please help with the following: I am trying to force an asp.net
Can any one post the details to develop restful web service in .net from
Can find why i get this error can someone help? package Android.data; public class
Can anyone help me trying to find out why this doesn't work. The brushes
Can you pls explain? I looked at this site for the data http://www.d.umn.edu/~gshute/java/statements.html while
Can anyone let me know how can we change the value of kendo combobox

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.