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
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/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.