Is ChatGPT-4 wrong? (JavaScript regex)

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?

ChatGPT Interaction:

User

let username = “Z97”;
let userCheck = /^\D[A-z]+\d*$/i; // Change this line let result = userCheck.test(username);

ChatGPT

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”:

  1. ^ will match the start of the string.
  2. \D will match the letter “Z”.
  3. [A-z]+ will match the letter “Z”.
  4. \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.

Therefore, result will be true.

\D[A-z]+ requires at least two letters. Or to be more precise one non-digit and one character from A-z range.

1 Like

To be honest, I’m not sure what the issue is you are describing above.

What regex?

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.

1 Like

Ahhh, of course! Silly me. Thank you very much for the concise explanation :smiley:

1 Like

regex:

/^\D[A-z]+\d*$/i

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 :slight_smile:

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 :laughing:

1 Like

am having an with this task aswell anyone willing to help me kindly?

/[1]{2,}[0-9]*$/
i dont know why this code aint working have tried everything


  1. a-zA-Z ↩︎

Hello and welcome to the community :smiley:!
Create your own post by clicking on the help button and then describe your issue.
We will then be glad to help:

ChatGPT-4 is pretty advanced, but like any tool or program, it’s not immune to errors. It’s designed to be highly accurate, but it can still make mistakes, especially with complex tasks like parsing natural language or intricate patterns. When it comes to JavaScript regex, while it can be very powerful, it’s also easy to make mistakes in the patterns you create.

1 Like