Can you find the bug in this piece of code? – RegExp edition 🌍

Hey there! 👋

I’m back with another installment of Find the bug, this time with Typescript/Javascript. Regular expressions are useful, but can behave in some unexpected ways. Can you tell me what the code below will output and what the cause for it is?…


This content originally appeared on DEV Community and was authored by Keff

Hey there! 👋

I'm back with another installment of Find the bug, this time with Typescript/Javascript. Regular expressions are useful, but can behave in some unexpected ways. Can you tell me what the code below will output and what the cause for it is?

!! Don't look at the comments to prevent spoilers if you want to solve it by yourself !!

Buggy code

const TEST_REGEXP = /[a-z0-9]+_[a-z0-9]+/gi;

function isValidName(value: any) {
    if (typeof value !== 'string') return false;

    return TEST_REGEXP.test(value);
}

const filenames = [
  "test_1",
  "test_1",
  "test_2",
  "other_test",
  "some_file"
];

for (let name of filenames) {
    console.log(isValidName(name));
}

 Now then, can you find the bug?


This content originally appeared on DEV Community and was authored by Keff


Print Share Comment Cite Upload Translate Updates
APA

Keff | Sciencx (2021-11-03T10:01:30+00:00) Can you find the bug in this piece of code? – RegExp edition 🌍. Retrieved from https://www.scien.cx/2021/11/03/can-you-find-the-bug-in-this-piece-of-code-regexp-edition-%f0%9f%8c%8d/

MLA
" » Can you find the bug in this piece of code? – RegExp edition 🌍." Keff | Sciencx - Wednesday November 3, 2021, https://www.scien.cx/2021/11/03/can-you-find-the-bug-in-this-piece-of-code-regexp-edition-%f0%9f%8c%8d/
HARVARD
Keff | Sciencx Wednesday November 3, 2021 » Can you find the bug in this piece of code? – RegExp edition 🌍., viewed ,<https://www.scien.cx/2021/11/03/can-you-find-the-bug-in-this-piece-of-code-regexp-edition-%f0%9f%8c%8d/>
VANCOUVER
Keff | Sciencx - » Can you find the bug in this piece of code? – RegExp edition 🌍. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/11/03/can-you-find-the-bug-in-this-piece-of-code-regexp-edition-%f0%9f%8c%8d/
CHICAGO
" » Can you find the bug in this piece of code? – RegExp edition 🌍." Keff | Sciencx - Accessed . https://www.scien.cx/2021/11/03/can-you-find-the-bug-in-this-piece-of-code-regexp-edition-%f0%9f%8c%8d/
IEEE
" » Can you find the bug in this piece of code? – RegExp edition 🌍." Keff | Sciencx [Online]. Available: https://www.scien.cx/2021/11/03/can-you-find-the-bug-in-this-piece-of-code-regexp-edition-%f0%9f%8c%8d/. [Accessed: ]
rf:citation
» Can you find the bug in this piece of code? – RegExp edition 🌍 | Keff | Sciencx | https://www.scien.cx/2021/11/03/can-you-find-the-bug-in-this-piece-of-code-regexp-edition-%f0%9f%8c%8d/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.