So I know ChatGPT can make mistakes, but I’m surprised that it’s disagreeing with freeCodeCamp’s output. I was surprised that this regex was still failing for one test in this lesson “Your regex should match the string Z97”, so I asked ChatGPT-4.
Can anyone tell me what ChatGPT is getting wrong here?
let username = “Z97”;
let userCheck = /^\D[A-z]+\d*$/i; // Change this line let result = userCheck.test(username);
This regex pattern /^\D[A-z]+\d*$/i is a little different than the previous one:
^ asserts the start of a line.
\D matches any character that’s not a digit.
[A-z]+ matches one or more letters (uppercase or lowercase). Note that using [A-z] can be problematic because it includes some non-letter characters that fall between Z and a in the ASCII table, but for this particular example, it doesn’t matter.
\d* matches zero or more digits.
$ asserts the end of a line.
i at the end makes the pattern case-insensitive.
Given the string “Z97”:
^ will match the start of the string.
\D will match the letter “Z”.
[A-z]+ will match the letter “Z”.
\d* will then match the digits “97”.
Given that all components of the regex pattern have found their respective matches in the string, the result of the test method will be true.
To be honest, I’m not sure what the issue is you are describing above.
If this is all you are giving ChatGPT then of course it is not going to agree with the actual challenge since there is more to the challenge then just this one regex.
Wouldn’t it make more sense to give ChatGPT all of the requirements for the challenge and then ask it to come up with the regex? I’m not suggesting anyone do that, since you should be solving these challenges without using ChatGPT. But unless ChatGPT has all of the necessary information then I don’t see how it can comment either way on the challenge.
The challenege involves creating only one correct regex and only one test was failing with the regex above:
“Your regex should match the string Z97"
This is why I only gave this small part to ChatGPT. I couldn’t understand why only this test was failing and I thought it may be able to point out why, as it’s provided me with surprisingly good explanations for similar situations as I’ve been making my way through the freeCodeCamp courses.
I didn’t want it to solve the whole challenge for me. I hit a wall and was looking for feedback, which @sanity so kindly provided
And I appreciate you taking the time to read the post and give some feedback @bbsmooth! I apparently need to structure future posts more clearly