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?…

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?


Print Share Comment Cite Upload Translate
APA
Keff | Sciencx (2024-03-29T13:18:08+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 2024-03-29T13:18:08+00:00,<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 2024-03-29T13:18:08+00:00]. 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 2024-03-29T13:18:08+00:00. 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: 2024-03-29T13:18:08+00:00]
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/ | 2024-03-29T13:18:08+00:00
https://github.com/addpipe/simple-recorderjs-demo