mardi 4 juin 2019

Strange behavior with regex and if statement [duplicate]

This question already has an answer here:

I am matching a string with regex, but I am experiencing a strange behavior. My code is as follows:

const regex = /^[\w-]*$/g;
const name = 'test22';

function isValidName(name) {
    return name && regex.test(name);
}

console.log(name, isValidName(name));

if (isValidName(name) === false) {
  console.log('not valid');
}

Upon running the code the following is logged on the console:

test22, true, not valid

So isValidName evaluates to be true, but the if statement runs nonetheless.

The goal of this task is to find strings that contain characters other than alphanumeric, numbers or dash. What could be the issue?

Aucun commentaire:

Enregistrer un commentaire