Restrict Possible Usernames

Hi, I am currently also stuck in this challenge and dont want to look up the solution. So you explained that | combines conditions with an OR. But I cannot for the life of me find an explanation online in how to combine with a strickt AND.

Could you give an example? It’s quite some time ago that I did the task.
Also in general please open your own topic when you got a question, keeps things more organized.

As an example look at this:
let username = “Jack%OfAllTrades”;
let userCheck = /[%]|[a]/g;
let result = userCheck.test(username);

console.log(result); //returns true

But if I try a regex like /[%][a]/g or /[%]&[a]/g it returns false. So how could I check both to be true here and not one or both with | ?

You don’t really need an “and” in a regular expression because a regular expression is a pattern. If I wanted a string to start with a number and end with a letter I would do ^\d.*[a-z]$

1 Like

I think that means I just don’t understand the concept at all then. In the fCC courses leading up to this challenge there was no concatenation of regex logic required.

If I take your example and you cannot use AND logic or if statements in the regex pattern, then how would I make a pattern that starts with a number AND ends with a letter like so ^\d.*[a-z]$ but doesen’t require a number at the beginning if the last letter is exactly T or something like that?

I tried solving the challenge in the meantime by turning the userCheck into an arrow function that asks simple non concatenating regex questions to the username devided by multiple if statements and returns such that the result in the end gives true or false to the challenges requirements. If I console log the Usernames, the result is as required, but my solution fails all tests still.

But I guess you are supposed to make everything happen in one regex format anyways and this attempt wouldnt be a true solution anyways.

it’s the regex that is being checked, so you will need to build a regex that works as requested

so you are checking if one condition OR an other condition appens
There is a way to check that, you have learned it through the regex section

I’m not entirely sure what you mean by “concatenation of regex logic”. A regular expression is always a pattern. abc means that the string contains “abc”. ^A+B means that it starts with one or more "A"s followed by a “B”. That’s not concatenation.

What you are describing is “or” logic. You want either something that starts with a number and ends with a letter or something that ends with the letter “T”. You can create groups within the pattern to offer alternatives. In your example T$|^\d.*[a-z]$. (Here is a regex101 example with tests.) Sometimes you want to use grouped sub-patterns, and sometimes you want to use something like lookarounds. It depends on the particular requirements of your pattern.

I don’t think there is an “and” for RegEx because the shortcuts are designed to be exclusive - meaning their union is empty. Thus an “and” wouldn’t make sense.

As for your example, I don’t actually understand what you try to test and would need an “and” for ^^°

Thanks for your replies. I think I can now formulate my problem better. I spent the last hours tinkering with regex stuff and even finished the certification project with the telephone number validator, wich I found much easier. The reason beeing that I just needed to format a regex wich validates the right numbers and then filter out numbers that don’t exactly fit the regex with a match check.

But in this challenge I am still completely lost, because if the regex finds only 1 letter in an invalid username, then the check returns true and I fail the test. I came up with something simple like this [a-z]{2,}\d?|[a-z]\d{2,}\i wich in my mind describes the restricted usernames perfectly. They either have to start with a minimum of 2 letters and can then have any numbers or they can start with 1 letter but then have to have a minimum of 2 numbers following that. But doing it like this, a string like BadUs3rnam3 still returns true, because the regex fits it partially. I would find it very intuitive to filter all out that don’t match exactly, but in this challenge I can’t and am therefore lost because I have no idea how to remove half fitting stuff without hard coding specific rules to cheat the tests. Hope my issue is understandable. Like in other words the string BadUs3rnam3 starts with valid letters, so I have no idea how a regex is supposed to not fit a single letter there but then also completely fit BadUsernam3 (without the middle 3).

First problem is [a-z] is only looking for lowercase letters. The other problem is ? is looking for zero or one digit, what if there where two or more digits? Other then that what you have is not far off from working solution to the test. There are anchors in regex that can specify the locations of things that may be helpful with this test.

edit: looking at the last bit i assume you are trying to use the case insensitive flag, but the syntax is a little off.

Yeah, I think I made a copy paste mistake, but I am using the i Flag and it works in testing.

I will have a close look at anchors and will try tackling this again in some days. Need a short break from it, as I was allready dreaming about regex this night.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.