yuu
yuu

Reputation: 5

iMacros FF Eval If Else .match .replace regex capturing group is ignored

SET !VAR1 EVAL("var s=\"{{!EXTRACT}}\";
if(!s.match(/.*>\s*(\d+).*/)) {s.replace=(/.*>\s*(\d+).*/,'$1');}
else {s.replace=/.*>\s*(\D+).*/,'$1';}")
PROMPT {{!VAR1}} 

I am trying to check whether the extracted HTML code (matching either the first or second snippet) matches a string of numeric characters "11111" outside the html tags and if it does, replace it with the 1st capturing group of said match ('$1'):

<td style="outline: 1px solid blue;">
              11111      <img src="/images/flaggen/de.png" name="" alt="" width="16" height="11" align="absmiddle">       SomeText                  (MoreText)                </td>

If it does not match, replace it with the string of non-numeric characters "SomeText" again using the first capturing group ('$1'):

<td style="outline: 1px solid blue;">
                        <img src="/images/flaggen/de.png" name="" alt="" width="16" height="11" align="absmiddle">       SomeText                  (MoreText)                </td>

The regex matches in both cases and does replace the string BUT only returns literally

$1

, or whatever I try to replace with.

No other regex I have tried worked (returns just the literal regex expression), neither did $&.

How can I sucessfully reference a capturing group in this expression?

Edit 1: Using $1 without quotes returns literraly $1, using double quotes "$1" returns iMacro error MacroSyntaxError: wrong format of SET command, line 42 (Error code: -910). Escaping the double quotes \"$1\" returns again literally $1.

Upvotes: 0

Views: 559

Answers (1)

Shugar
Shugar

Reputation: 5299

If I understood you right, try the following EVAL command:

SET !VAR1 EVAL("var s='{{!EXTRACT}}'; if(s.match(/.*>\s*(\d+).*/)) {s = s.match(/.*>\s*(\d+).*/)[1];} else {s = s.match(/.*>\s*(\D+)\\(/)[1].trim();} s;")

Upvotes: 1

Related Questions