[SOLVED]: Need help with username regex in Javascript to limit the character count | SoloLearn: Learn to code for FREE!

+20

[SOLVED]: Need help with username regex in Javascript to limit the character count

/* * Username regex requirements: * 1. Must start with alphabets any case. * 2. Must end with digits * 3. Cannot have alphabets in between the digits. * 4. Should be between 7 to 12 characters long. */ I am finding it difficult to add character count regex ( []{7, 12}) to existing regex( shared in code). I have added a test scenario for the regex that should pass all positive and negative test scenarios. https://code.sololearn.com/WKPRxtflnUEk/?ref=app

8/9/2020 10:09:28 PM

Morpheus

10 Answers

New Answer

+19

const usernameRegex = /^(?=.{7,12}$)[a-zA-Z]+\d+$/

+12

John Wells I would use your pattern as is myself. A more relaxed alternatives could be: /^(?=.{7,12}$)[a-z]+\d+$/i But then the case insensitive flag must be known to be included with the pattern. I just thought I'd post it anyway for others to review. Morpheus Incidentally... this was similar to the MongoDB query I did on Friday with you guys in testing the length with the positive lookahead. 😉 In our query, we were looking to validate that all the Salesforce Customer IDs we had used the following format: - 18 characters - Started with sequence of numbers - Followed by alphanumeric characters - Ended with a letter

+10

Thanks 😊 John Wells . David Carroll I remember our Salesforce exercise but usecases of positive/negative lookahead is still tripping me up. 😪 While trying to solve this i was thinking like this. - Need to do this && operation somehow (^[a-zA-Z]+\d+$) && ([a-zA-Z\d]{7, 12}) I was doing Google searches on how to do && coz I knew we do have a or ( | ) operator. Then it turned out that by default all regex operations are in &(and) mode itself. - when using lookahead and look back operators my current mindset is - "Use them only when we need to match based on previous or following string" Use of positive lookahead like this is still tripping me up. 🥴 Here we are applying 2 regex on same string but how? Regarding (?=.{7,12}$) - whats the use of $ here? - Shouldn't this lookahead part correspond to the text coming after our intended text?

+6

Morpheus Indeed... lookarounds will bend your mind a bit at first. 😉 The $ is the end of line anchor. Likewise, the ^ is the start of line anchor. When using both anchors in the lookaround: (?=^ ... $) - excluding the three dot ellipses The positive lookahead essentially prequalifies the string before matching on the pattern that follows. NOTE: The lookaround is sort of like a peek into the string before running the actual pattern match on the string. Positive look ahead or look behind with positive pattern matching effectively accomplishes your (this) && (that) check. UPDATE: I removed the comments about (this) || (that) because the lookaround isn't needed to effectively accomplish the OR scenario.

+2

David Carroll John Wells

+1

^(?=.{7,12}$)[a-zA-Z](?=\w)[a-zA-Z]+(?=\d)\d+\d$ Here's mine!

+1

^(?=.{7,12}$)^[a-zA-Z]+\d+$ 1) ^(?=.{7,12}$) we used lookhead ?= we have to notice that nothing is behind the ?= that means match this .{7,12}$ condition to our string when nothing is behind the string. So nothing is behind the string means behind the first index,then our condition started to match from the first index to the last index. $ used to match the characters in the end. so whats in the end? our whole string is in the end just next to nothing. 2) ^[a-zA-Z]+ thats another condition mean our string must be start with a letter and have one or more alphabets. I think John Wells miss the ^ before this condition.so if we miss ^ than we can write numbers in the start as well. 3)\d+$ our string must be end with one or more number. Use of $ means our numbers must be in the end I don't know my answer is correct or not but i just told you what i learned

+1

Solved:Need help with search bar and a creative login form https://code.sololearn.com/WbnXcXYKNssX/?ref=app

0

([a-zA-Z0-1\d]{minrange,maxrange})

-7

Hi mam