-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?

0

1 Answer 1

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);

Sign up to request clarification or add additional context in comments.

10 Comments

@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)
the snippet is correct, try run snippet
@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)"
That's because your regex engine doesn't support lookbehind groups.
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?
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.