Restrict Possible Usernames

Hi, I have a question. Why my code no pass?

let username = "JackOfAllTrades";
let userCheck = /^[a-zA-Z]+[a-zA-Z]\d*$/ig; // Cambia esta línea
let result = userCheck.test(username);

error: Your regex should match the string Z97

I’m very confussed. Please help me

Can you explain a little more about what is confusing you. The error is telling you that your pattern should allow “Z97” but it does not. Do you understand why it doesn’t?

Also, you might want to review the challenge Match a Literal String with Different Possibilities. You can use the pipe operator with more than just strings.

No, I don’t understand. Can you explain me?

Explain to me in plain words what your expression is doing:

/^[a-zA-Z]+[a-zA-Z]\d*$/

^[a-zA-Z] start with and Alphabet character
+
[a-zA-Z] Alphabet character \d*$ ends with a number

but I can’t link them together. I try with a | operator between them but my code have more errors

Your current pattern:

/^[a-zA-Z]+[a-zA-Z]\d*$/

Matches two or more letters at the beginning of the string and then 0 or more numbers after those letters, so it will not catch the string “Z97” because your pattern requires at least two letters at the beginning.

You are actually off to a good start. This pattern catches almost everything except for “Z97”. So you need to add something to catch the string with only one letter at the beginning and numbers afterwards.

And as I hinted to earlier, you can use the pipe operator to do this.

I think something about:

/^[a-zA-Z]+[a-zA-Z]\d*$|^[a-zA-Z]+\d*$/

but it’s wrong

You are so close. Your second pattern is too permissive. Read the fourth requirement again.

/^[a-zA-Z]+[a-zA-Z]\d*$|^[a-zA-Z]+\d*$/

I think that, but it’s wrong too :frowning:

I don’t see any changes. The second pattern is too permissive. You need to tighten it up a little.

according to the 4 step. I think in:

/^[a-zA-Z]+[a-zA-Z]\d*$|^[a-zA-Z]+[a-zA-Z]/

but my code still not pass. I’m very frustated. I’m sorry about that

ok. I passed the exercise.

`/^[a-zA-Z]+[a-zA-Z]\d*$|^[a-zA-Z]+[\w]\d$/`

Thanks a lot and I hope to help somebody. Cheers

1 Like