0

I am trying to create a Regular Expression from code. Basically taking a code snippet (even if it is only a part of a line) and search for an exact match in a code base. I am using RegExp to create a regular expression that I pass to another function. An example:

let codeSnippet = new RegExp("let value = myFunc('something')");

This works great. However, it seems that if there is something unterminated in the code, then I can't make a RegExp from it. So this doesn't work:

let codeSnippet = new RegExp("let value = myFunc('someth");

I want to have a RegExp that is exactly what is passed in, as I am trying to match exactly. I am getting this error:

SyntaxError: Invalid regular expression: /let myfunc(\"some/: Unterminated group

How can I create a RegExp that is just exactly what is entered, no matter what?

Lee Morgan
  • 550
  • 1
  • 6
  • 26
  • 2
    You need to escape special characters like parentheses. – Barmar Jun 28 '23 at 19:41
  • 2
    Why are you using a regexp if you want to search for exactly matches? You can just use `String.includes()` – Barmar Jun 28 '23 at 19:42
  • I'm not searching a string. I am searching large numbers of files. So I am using a different tool that requires a regex. I thought RegExp was supposed to do that. I'll give it a shot. Thanks – Lee Morgan Jun 28 '23 at 19:47

0 Answers0