Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to get data from string using Javascript Regex
    primarykey
    data
    text
    <p>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.</p> <p>String:</p> <pre><code>&lt;a href="/l.php?u=http%3A%2F%2Fbit.ly%2FPq8AkS&amp;amp;h=aAQFZxdL0&amp;amp;s=1" target="_blank" rel="nofollow nofollow" onmouseover="LinkshimAsyncLink.swap(this, &amp;quot;http:\\/\\/bit.ly\\/Pq8AkS&amp;quot;);" onclick="LinkshimAsyncLink.referrer_log(this, &amp;quot;http:\\/\\/bit.ly\\/Pq8AkS&amp;quot;, &amp;quot;http:\\/\\/www.facebook.com\\/si\\/ajax\\/l\\/render_linkshim_log\\/?u=http\\u00253A\\u00252F\\u00252Fbit.ly\\u00252FPq8AkS&amp;amp;h=aAQFZxdL0&amp;amp;s=1&amp;quot;);"&gt;http://bit.ly/Pq8AkS&lt;/a&gt;&lt;/div&gt;&lt;div class="shareUnit"&gt;&lt;div class="-cx-PRIVATE-fbTimelineExternalShareUnit__wrapper"&gt;&lt;div&gt;&lt;div class="-cx-PRIVATE-fbTimelineExternalShareUnit__root -cx-PRIVATE-fbTimelineExternalShareUnit__hasImage"&gt;&lt;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;amp;share_id=271663136271285&amp;amp;max_width=403&amp;amp;max_height=403&amp;amp;context=timelineSingle" rel="async" href="#" onclick="CSS.addClass(this, &amp;quot;-cx-PRIVATE-fbTimelineExternalShareUnit__loading&amp;quot;);CSS.removeClass(this, &amp;quot;-cx-PRIVATE-fbTimelineExternalShareUnit__video&amp;quot;);"&gt;&lt;i class="-cx-PRIVATE-fbTimelineExternalShareUnit__play"&gt;&lt;/i&gt;&lt;img class="img" src="http://external.ak.fbcdn.net/safe_image.php?d=AQDoyY7_wjAyUtX2&amp;amp;w=155&amp;amp;h=114&amp;amp;url=http%3A%2F%2Fi1.ytimg.com%2Fvi%2FDre21lBu2zU%2Fmqdefault.jpg" alt="" /&gt;&lt;/a&gt; </code></pre> <p>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...</p> <pre><code>Body = document.getElementsByTagName("body")[0].innerHTML; regex = /2Fbit.ly%2F(.{6})&amp;amp;h/g; Matches = regex.exec(Body); </code></pre> <p>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 <code>"&amp;amp;h"</code> in the string above instead of stopping at the first one it hits. </p> <pre><code>Body = document.getElementsByTagName("body")[0].innerHTML; regex = /2Fbit.ly%2F(.*)&amp;amp;h/g; Matches = regex.exec(Body); </code></pre> <p>So basically the main part of the string i'm trying to focus on is <code>"%2Fbit.ly%2FPq8AkS&amp;amp;h"</code> so that I can get the "Pq8AkS" out of it. When I use the <code>(.*)</code> it's grabbing everything between <code>"%2F"</code> and the very last <code>"&amp;amp;h"</code> in the large string above.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload