Search code examples
javascriptregeximacros

Extracting Attribut Value Imacros With Regex


I was just learning RegEx in iMacros and would like to extract the ID of this user.

<a href="https://web.facebook.com/guruBKjualan?fref=gm&amp;dti=634080336761016&amp;hc_location=group" data-hovercard="/ajax/hovercard/user.php?id=1481093529&amp;extragetparams=%7B%22fref%22%3A%22gm%22%2C%22directed_target_id%22%3A634080336761016%2C%22dti%22%3A634080336761016%2C%22hc_location%22%3A%22group%22%7D" data-hovercard-prefer-more-content-show="1" aria-controls="js_c" aria-haspopup="true" aria-describedby="js_d" id="js_e">Decky Zulkarnain</a>

In this example, extracted data should be 1481093529

The method using of course by extracting html code, and then find matched value and extract it.

I am trying this

SET !VAR2 EVAL("var s=\"{{!EXTRACT}}\"; s.match(/user.php?id=[\"'](\d+?)[&]/)[1];")

But no luck. After few times, I got this.

SET !VAR2 EVAL("var s=\"{{!EXTRACT}}\"; s.match(/data-hovercard=[\"'](.+?)[\"']/)[1];")

It does get data-hovercard attribute in a whole. Just not what I am exactly needed. But How could my previous try not working? Am I missing something?

Thanks


Solution

  • Try

    /(?<=(id=)).*(?=&)/g
    

    Explanation

    • (?<=(id=)) to match but not capture as a match id=
    • .* to match all characters
    • (?=&) to match & but don't remember the match.

    Demo

    var input = '<a href="https://web.facebook.com/guruBKjualan?fref=gm&amp;dti=634080336761016&amp;hc_location=group" data-hovercard="/ajax/hovercard/user.php?id=1481093529&amp;extragetparams=%7B%22fref%22%3A%22gm%22%2C%22directed_target_id%22%3A634080336761016%2C%22dti%22%3A634080336761016%2C%22hc_location%22%3A%22group%22%7D" data-hovercard-prefer-more-content-show="1" aria-controls="js_c" aria-haspopup="true" aria-describedby="js_d" id="js_e">Decky Zulkarnain</a>';
    var regex = /(?<=(id=)).*(?=&)/g;
    var output = input.match( regex );
    
    if ( output )
    {
      console.log( output[ 0 ] );
    }