Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Undefined RegExp always match #37116

Closed
miguelaeh opened this issue Jan 28, 2021 · 1 comment
Closed

Undefined RegExp always match #37116

miguelaeh opened this issue Jan 28, 2021 · 1 comment

Comments

@miguelaeh
Copy link

  • Version: 12.20.0

  • Platform: Linux 4.15.0-132-generic Introduce the Nil Object #136-Ubuntu SMP Tue Jan 12 14:58:42 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux

  • Subsystem:

What steps will reproduce the bug?

Hi, I am not totally sure if this is a bug or if it is expected, but it seems to be a problematic behavior.
Execute the following NodeJS code. Creting a RegExp with an undefined variable will always match any string:

const str="hello";
const regex = undefined;
str.match(new RegExp(regex));

Captura de pantalla de 2021-01-28 21-39-06

How often does it reproduce? Is there a required condition?

Always

What is the expected behavior?

I am not totally sure if this is unexpected, but IMHO I would expect an error when trying to create an undefined regular expression.

What do you see instead?

I see that the undefined regex matches always any string. It produces unexpected outputs if you use it, for example, to test a match inside an if.

Additional information

@cjihrig
Copy link
Contributor

cjihrig commented Jan 28, 2021

undefined is treated as the empty string in the RegExp constructor. The JavaScript specification dictates this behavior, and it is implemented in V8, so it is out of Node's control.

@devsnek devsnek closed this as completed Jan 28, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants