Alternate Solution to RegExp Problem?

Alternate Solution to RegExp Problem?

I think this is also another valid solution to the challenge, and doesn’t need the starting ^.

let username = "JackOfAllTrades";
let userCheck = /([a-z])([a-z]+|[0-9][0-9]+)$[0-9]*/i; // Change this line
let result = userCheck.test(username);


If you don’t specify starting ^ you don’t check the start of the string:

// etc...

So it’s not a valid solution, it’s a valid hack of insufficient tests

1 Like

Got it, thanks for the example!

Please include the use of parentheses in the curriculum.
This seems to work:
let userCheck = /^[A-z][\d][\d]+$|^[A-z][A-z]+\d*$/;

this works too, not sure if its ok
let userCheck = /^[\D]([\d]{2,}|[\D]+\d*)$/i

This one worked for me: /(^\D{2,}$)|(^\D+\d{2,}$)/

The breakdown for me was:

  1. If it must start with a letter, then I should use ^\D.
  2. Since I know that the minimum length is 2 and that requires 2 letters, then the first thing I need to check for is ^\D{2,} at a minimum.
  3. In all other cases, I know that the username must start with a letter and, if it has a number, it needs to end with the number. In this case, I figured that ^\D+\d{2,}$ would be the best option.
  4. Since these are two different scenarios for the username, and I didn’t want to have one that was super long, I used the | to say that it could be either.

Not sure if this will work for all scenarios, but it worked for this one.

Wrongly sorted. This problem is out of sequence, it requires the use of “number of matches” among other things that are only seen later.

Following code will help :-

let username = "JackOfAllTrades";
let userCheck = /^[a-z](\d\d+|\D+\d*)$/i;
let result = userCheck.test(username);