1

I have the following RegEx that works:

var expression = /s\[0\]\[children\]\[.*?\]\[/g;
var replace_string = "s[" + count + "][children][" + subCount + "]";
$input.attr('name', $input.attr('name').replace(expression, replace_string));

Now I am wanting to replace the 0 in the above regex with a variable, "count". I have read up that it needs to be turned into a string, which I have done:

var expression = "/s\\[" + count + "\\]\\[children\\]\\[.*?\\]/g";

But that doesn't want to work for some reason, what am I doing wrong?

Cheers

2
  • What is the expected output? Commented Jun 29, 2015 at 5:20
  • nucleo_isotope_gallery_filters[0][children][0][preset_referrer] nucleo_isotope_gallery_filters[0][children][1][preset_referrer] nucleo_isotope_gallery_filters[0][children][2][preset_referrer] etc.. Commented Jun 29, 2015 at 5:21

1 Answer 1

2

You need to use RegExp constructor whenever you want to call a variable from regex.

var expression = new RegExp("s\\[" + count + "\\]\\[children\\]\\[.*?\\]", "g");
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.