Ad
  • Default User Avatar

    thanks for the reply this is new to me and i'm sure it will come in handy later

  • Custom User Avatar

    Regex is used to test (using Boolean values) and manipulate strings in many different programming languages such as JavaScript, Python, Java, C++ and more. Overall, the code looks through each item in the array and checks whether the username is validate. If all the usernames are valid, then return true, if not all of them are valid return false.

    1.)

    /^(?=.*\d+)(?=.*[a-z])[a-z\d]{6,10}$/g
    
    ^ asserts position at start of the string
    Positive Lookahead (?=.*\d+)
    Assert that the Regex below matches
    .* matches any character (except for line terminators)
    * Quantifier — Matches between zero and unlimited times, as many times as possible, giving back as needed (greedy)
    \d+ matches a digit (equal to [0-9])
    + Quantifier — Matches between one and unlimited times, as many times as possible, giving back as needed (greedy)
    Positive Lookahead (?=.*[a-z])
    Assert that the Regex below matches
    .* matches any character (except for line terminators)
    * Quantifier — Matches between zero and unlimited times, as many times as possible, giving back as needed (greedy)
    Match a single character present in the list below [a-z]
    a-z a single character in the range between a (ASCII 97) and z (ASCII 122) (case sensitive)
    Match a single character present in the list below [a-z\d]{6,10}
    {6,10} Quantifier — Matches between 6 and 10 times, as many times as possible, giving back as needed (greedy)
    a-z a single character in the range between a (ASCII 97) and z (ASCII 122) (case sensitive)
    \d matches a digit (equal to [0-9])
    $ asserts position at the end of the string, or before the line terminator right at the end of the string (if any)
    
    Global pattern flags
    g modifier: global. All matches (don't return after first match)
    

    from https://regex101.com/

    2.) "?=" is a special regex group called a "positive lookahead" which matches at a position where the pattern inside the lookahead can be matched (it matches the object before the lookahead).

    3.) .test(x) returns a Boolean value (true or false) based on the regular expression. It tests whether that specified regex appears in the string at all anywhere in the string. If it appears at least once then return true else false.