0

I am a new to regex and need a little help. I can't seem to make it work when I try to concatenate on line 3.

var method = function(name, url) {
   name = name.replace('/[\[]/', '\\[').replace('/[\]]/', '\\]');
   var regex = new RegExp('[\\?&]' + name + '=([^&]*)'); 
    var results = regex.exec(url);
    return results;
};

What am I missing here? Your help is greatly appreciated.

var regex = new RegExp('[\\?&]' + name + THIS PART IS NOT BEING CONTCATENATED.
Barmar
  • 741,623
  • 53
  • 500
  • 612
Jak Dev
  • 11
  • Do you have some example input and expected result? Also, you can inspect the regex you constructed with `.toString()` – Peter Thoeny Sep 22 '22 at 23:35
  • Why do you think the concatenation isn't working? The problem is that `name.replace()` isn't working because you quoted the regexp literals. – Barmar Sep 22 '22 at 23:36
  • You can do both replacements at once with `name = name.replace(/[][]/g, '\\$&')` – Barmar Sep 22 '22 at 23:38
  • Your concatenation is working fine, but you are not escaping `name` correctly. See attached post how to do it. – Wiktor Stribiżew Sep 23 '22 at 09:44

0 Answers0