Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why this regex doesn't work in vscode but works in another site?

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

                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.

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading