Regex pattern returns true, while inside function returns false

Hello everyone,

I am working on a little project to dive deeper into HTML,CSS and Javascript and I came across rpoblem in one of my functions, where I use a fully working regex pattern. I tested it in console.log, it always returns true for the strings i am checking.
Here is the pattern:
const regex = /^[\p{L}\s]+$/giu;

Now my function in which i am using it always returns true, according to console.log.
Here is the code for the function:
function validateFirstName(firstName) {
    const regex = /^[\p{L}\s]+$/giu;
    console.log(firstName);
    console.log('firstname validation with regex',regex.test(firstName));
    return regex.test(firstName);
};

Is there something I don't understand here, because when i run the following if statement or simply logging the validateFirstName function, it returns false, regardless the regex pattern.
Here is how i checked:
console.log('Before validateForm');

    if (!validateFirstName(firstName)) {
        console.log('Validation failed');
        alert('First Name must consist of letters and/or spaces');
        return false;
    }

    console.log('Validation passed');
Was this page helpful?