This post is getting a better understanding of regex concepts in Javascript and not so much of a critique of how crappy the code is. The exercise is to use 'if' statements to check if boolean conditions are met with .test() booleans and output messages to the user. Below is the exercise code;
Now when you set b = 'aA@2' it doesn't pass the requirement test even though console.log proves that all the boolean conditions pass. But if you set b = 'aA@@2'; it passes. It seems as if you have to put at least 2 special characters in or 2 numbers in to pass. and if you let b = 'aA@@'; it passes but should fail.
Why doesn't this work the way it should?
I know that there are more modern and precise ways to use reg ex but I want to know why the if statements don't work as expected. This might help out others, too.
Thanks for your inputs.
let a = document.getElementById('is');
let b = 'aA@2';
let regCharTestSmall = /[a-z]/g;
let regCharTestBig = /[A-Z]/g;
let whiteSpaceTest = /\s/g;
let digitTest1 = /[0-9]/g;
let specCharTestNonWord = /\W/g;
console.log(regCharTestSmall.test(b));
console.log(regCharTestBig.test(b));
console.log(whiteSpaceTest.test(b));
console.log(digitTest1.test(b));
console.log(specCharTestNonWord.test(b));
console.log(b.length);
console.log(b);
console.log(typeof b)
function regExTester(stringTest) {
if (regCharTestSmall.test(stringTest) === false) {
a.innerHTML = "You must have at least one lower case letter in your entry.";
}
if (regCharTestBig.test(stringTest) === false) {
a.innerHTML = "You must have at least one upper case letter in your entry.";
}
if (whiteSpaceTest.test(stringTest) === true) {
a.innerHTML = "You cannot have a white space in your entry";
}
if (digitTest1.test(stringTest) === false) {
a.innerHTML = "You must have at least one number in your entry!";
}
if (specCharTestNonWord.test(stringTest) === false) {
a.innerHTML = "You must have at least one special character in your entry!";
} else
a.innerHTML = `You typed in: ${b} and passed`;
return;
}
regExTester(b);
.as-console-wrapper {
height: 25px;
opacity: 0.2;
}
.as-console-wrapper:hover {
height: auto;
opacity: 1;
}
<h1>Reg Ex</h1>
<span>Did Regex Pass Requirements?: <span id="is"></span></span>