JavaScript Regex challenge confusion

JavaScript Regex challenge confusion
0

#1

Hello everyone, first time posting on this forum as I am looking for some assistance understanding what is happening with my current code. I passed the challenge but do not feel that my code is ensuring that the password contains more than 5 characters, perhaps I am wrong but if someone could help me break down what happens in here and if my code actually checks if a password contains 5 or more characters. I used an online IDE for my code to check it myself and it returned “True” even if it contains less than 5 characters. Apologies in advance if this post isn’t properly formatted, thank you all! :slight_smile:

Here is my code:

let sampleWord = "a12";

let pwRegex = /(?=[A-Z+])(?=\w\d{2})/i; // Change this line

let result = pwRegex.test(sampleWord);

#2

If you haven’t checked out regex101, it does a great job breaking a pattern down for you. Here is your expression with their breakdown.


#3

You can use an online tool to check the logic of your regular expressions, e.g. https://regex101.com/.
I am not sure what exactly you want to do. However, currently your regex consists of 2 positive lookaheads.
The first one says that the password must contain either a character from A-Z or the + character.
The second one says that the password must contain an alphanumeric character followed by 2 digits.
And you’ve applied the case insensitive flag, so e.g. the first lookahead checks a-z as well.

@ArielLeslie beat me to the post :slight_smile:


#4

I did not know about this resource thank you for that! It does create a bit of further confusion now haha, I am looking at the breakdown of it and it tells me that the + inside the expression (?=[A-Z+]) is actually included in what the regex is looking for which is not something I wanted in the first place, so that part is very helpful to know. My big concern here is that I did pass the challenge but if i was to have less than 5 characters in total my code would still pass the test. Challenge I hope what I’m asking makes sense! :slight_smile: