2

I don't know much about regex, but I'm trying to use this regex made by chatGPT in vscode but it returns nothing as result. But when I try to use in another site like https://regex101.com/, the string matches.

Regex:

throw\s+new\s+ApiResponseError\s*\(\s*HttpStatusCode\.([^,]+),\s*('[^']*'|"[^"]*"),\s*new\s+Error\(`([^`]*)`\),\s*(true|false)?\s*\)

Pattern that matches in the site:

                throw new ApiResponseError(
                    HttpStatusCode.BAD_REQUEST,
                    'low',
                    new Error(`Required parameters of the '${worksheetDefinitions.worksheetName}' worksheet were not informed`),
                    false
                )

Note the blank spaces, i need them in the regex as well.

Is there any configuration I need to do?

I tried to change the regex, search for solutions but it keeps returning nothing. I expected that the regex works like in the site.

  • Yeah, you need to use `\r` or `\n` inside your regex to tell the regex engine it can match across lines. Well, here you can add `\n{0}` or `\r{0}` at the pattern start, this will work in any scenario. – Wiktor Stribiżew May 05 '23 at 14:33
  • Oh it works like a charm! Thanks man. I'm new here as well, how do I mark your answer as correct? – Gabriel Henrique May 05 '23 at 14:37
  • I tried to mark the answer as useful too but it gives me the message: "Thanks for the feedback! You need at least 15 reputation to cast a vote, but your feedback has been recorded." Sorry man :( – Gabriel Henrique May 05 '23 at 14:46
  • Correct, SO features are unlocked with reputation gains. No need to be sorry. – Wiktor Stribiżew May 05 '23 at 14:50

2 Answers2

2

You can make any regex for Visual Studio Code match across line breaks if you specify \n{0} anywhere inside the pattern, the most convenient places are start and end of the pattern. I'd recommend putting it at the start so as not to break any specific regex constructs if you are not familiar with regular expressions that much.

So, the rule is \n{0} + YOUR_REGEX.

VSCode regex engine requires \n to be present in the pattern so that any construct like \s or [^"] could match line break chars, and defining a "match zero newlines" pattern does the job.

In your case, you can use

\n{0}throw\s+new\s+ApiResponseError\s*\(\s*HttpStatusCode\.([^,]+),\s*('[^']*'|"[^"]*"),\s*new\s+Error\(`([^`]*)`\),\s*(true|false)?\s*\)
^^^^^

If you have other issues matching any character with a VSCode regex, see Multi-line regular expressions in Visual Studio Code.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
1

Add [\s\n]* to your regex like this:

throw\s+new\s+ApiResponseError[\s\n]*\(\s*HttpStatusCode\.([^,]+),\s*('[^']*'|"[^"]*"),\s*new\s+Error\(`([^`]*)`\),\s*(true|false)?\s*\)

which as @Wiktor mentioned is necessary because vscode handles newlines in a different way than regex engines. For performance reasons you need to explicitly tell vscode to look for newlines with a literal \n not just \s (which would otherwise include newlines but doesn't here in vscode).

So using [\s\n]* tells vscode to look for newlines as well as the other \s operstions.

InSync
  • 4,851
  • 4
  • 8
  • 30
Mark
  • 143,421
  • 24
  • 428
  • 436