-1

I am trying to perform a search and replace in a string using javascript. An example of the string looks like this: Parameter 1 = xxx where xxx are some characters that I want to replace

The RegEx needs to find everything after "Parameter 1 = " until the end of the line, which is xxx. The javascript then needs to replace xxx it with yyy, where yyy are some other characters. For example, Parameter 1 = 42 => Parameter 1 = 11

I found the regex for this on this forum (What Regex would capture everything from ' mark to the end of a line?). Modifying it for my case would be:

(?<=Parameter 1 = ).*$

I then tried the following java script:

var str="Parameter 1 = 42";`
var res=str.replace(/(?<=Parameter 1 =).*$/, "11");
alert(res);

However, that gives me an error when I run it using iMacros:

SyntaxError: invalid regexp group, line 6 (Error code: -991)

p.s. I do not understand why my question has been marked as [duplicate] and closed?

viking
  • 1
  • 1

1 Answers1

0

Here is what you want:

var str="Parameter 1 = 42";
var res=str.replace(/(?<=Parameter 1 =).*$/, "11");
console.log(res);

And if lookbehind aren't supported you can do this:

var str = "Parameter 1 = 42";
var res = str.replace(/(Parameter 1 =)(.*)$/, "$163");
console.log(res);
8HoLoN
  • 1,122
  • 5
  • 14
  • @HoLoN, Thank you for your answer. However, your solution also gives me an error: SyntaxError: invalid regexp group, line 6 (Error code: -991) (I am running the javascript in iMacros) – viking Jul 26 '20 at 09:17
  • the snippet is correct, try `run snippet` – 8HoLoN Jul 26 '20 at 10:14
  • @HoLoN, Yes, the snippet works. However, it doesn't work in iMacros (v. 8.9.7 and Firefox Developer Edition (Portable) v. 55.0b2 (64-bit)). I get the Syntax error above: "SyntaxError: invalid regexp group, line 6 (Error code: -991)" – viking Jul 26 '20 at 18:22
  • That's because your regex engine doesn't support lookbehind groups. – 8HoLoN Jul 26 '20 at 20:26
  • Oh. Is there some other way of solving this in iMacros? Maybe rewriting the RegEx somehow so that it is compatible with iMacros RegEx engine? – viking Jul 27 '20 at 00:32
  • My answer got updated – 8HoLoN Jul 27 '20 at 07:09
  • Thank you again! However, when I run the snippet, I get Parameter 1=1, instead of Parameter 1=11. I get the same incorrect result in iMacros but w/o the Syntax error :-)!! I don't understand the RegEx, but by trial and error, I found that var "res = str.replace(/^(Parameter 1 =)(.*)$/, "$01yyy")" worked. For example if yyy=11, I get Parameter 1=11. Is this correct, should I use $01 + new yyy value? (Sorry for being a newbie at this but how does the $01 "prefix" work?) – viking Jul 27 '20 at 08:10
  • Ok that good now, could you accept/upvote the answser ? – 8HoLoN Jul 27 '20 at 08:15
  • I see that you corrected your answer. Can you please explain how the $1 "prefix" works? Also, do you know who to contact for clarification on why my question was downvoted, marked as duplicate and closed? I upvoted your answer, but it still shows as 0 (apparently because my own score <15). Sorry! – viking Jul 27 '20 at 17:54
  • $x make a reference to the captured group in regex with x starting at 1. – 8HoLoN Jul 28 '20 at 15:01