1

I want a regex to grab the arguments of RegExp without parsing the code.

RegExp(/a+b/)       Output: /a+b/
RegExp(/a+b/, 'g')  Output: /a+b/, 'g'

I have came up with the following regex:

(?:RegExp)\((.*)\)

It works fine for most examples, but fails for some cases (e.g. minified, 1 line JavaScripts, regexs including parentheses):

RegExp("myregex")                               - capturing group 1: "myregex" 
Regexp("something"); myfunction("something")    - capturing group 1: "something"); myfunction("something"
RegExp("my(regex)")                             - capturing group 1: "my(regex

This is clearly due to finding a matching closing parenthesis. Is there a workaround for this?

krillgar
  • 12,596
  • 6
  • 50
  • 86
GRoutar
  • 1,311
  • 1
  • 15
  • 38
  • 1
    Use the lazy quantifier: `(.*?)` instead of `(.*)` – michalwa Feb 14 '20 at 13:44
  • 1
    What if the regular expression includes a parenthesis? – Pointy Feb 14 '20 at 13:46
  • Does this answer your question? [Regular Expression to get a string between parentheses in Javascript](https://stackoverflow.com/questions/17779744/regular-expression-to-get-a-string-between-parentheses-in-javascript) – Eldar Feb 14 '20 at 13:54
  • 2
    RegExp is not the right solution for this. – epascarello Feb 14 '20 at 13:57
  • Theoretically, you can have as many opening and closing paretheses within your regex, so you using a regular expression for this problem cannot possibly be always correct. – M0nst3R Feb 14 '20 at 13:58
  • Have you considered looking into creating a special parser function that will look for opening and closing parentheses within your regex and only stopping when it actually finds the closing parenthese of the `Regexp` call? – M0nst3R Feb 14 '20 at 14:00
  • That's a different question @Eldar – GRoutar Feb 14 '20 at 14:02
  • I have came up with this: `(?:RegExp)\(["'/](.*?)["'/]\)` https://regex101.com/r/sD7iOd/2 It doesn't work if a variable name is passed, but I don't really need it in those cases, so I guess this acceptable for my use case. – GRoutar Feb 14 '20 at 14:04
  • @GRoutar is the RegExp call always at the end of a line? That's about the only way I can imagine you'll find a working regex without a recursive engine. – Zaelin Goodman Feb 14 '20 at 14:41
  • @ZaelinGoodman Nope, not necessarily. – GRoutar Feb 14 '20 at 15:03
  • @GRoutar I just think it's really to complicated to get all the possibilities working... I just added a `\b` in front of `RegExp` and changed to content to see that there are so many risks at doing this with a regular expression: https://regex101.com/r/sD7iOd/4 – Patrick Janser Feb 14 '20 at 16:03
  • @GRoutar Perhaps it could be good to find all the RegExp() calls and see what they all look like in the content you are working with. You'll then be able to simplify things by only handling what you actually have as variants. – Patrick Janser Feb 14 '20 at 16:08

1 Answers1

0

Try this RegEx:

/.*RegExp\((\"(?:.+?)\"|\/(?:.+?)\/.*)\).*/gmi

Review and test it out here: https://regex101.com/r/cReoV7/6

rbonestell
  • 420
  • 4
  • 13
  • @GRoutar I noticed and addressed the defect of not capturing parenthesis which should have otherwise been included. – rbonestell Feb 14 '20 at 16:48