2

There is a regex in my project,and i am trying to understand what does it mean,so if any one can help me out

/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$/

and

/^(?=.*[0-9])(?=.*[a-zA-Z])([a-zA-Z0-9]+)$/

In first I know the first one ,but what does (.[_a-z0-9-]+)* this is doing and what does this do (.[a-z0-9-]+)*(.[a-z]{2,4})$

and in second what does this ?= is doing in this expression

$("#mailId").change(function(){
                    var m=$("#mailId").val();
                    var mailRegex=/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$/; 
                    if(m=="")
                    $("#errMail").html("MailId can't be empty.");
                    else if(m.match(mailRegex))
                    $("#errMail").html("");
                    else
                        $("#errMail").html("MailId is not valid.");
                });
user3380123
  • 703
  • 1
  • 6
  • 14

4 Answers4

3

The first seems an email validation for addresses in a specific form:

(name).(name).(name) ... @ (name).(name).(name) ... .(domain)

where name is a sequence of a..z, numbers or the charater - and domain is a sequence of 2 to 4 letters. In the specific:

  • ^ = start of string
  • [_a-z0-9-]+ = one or more of underscore, lowercase, number or dash
  • \. = a dot
  • [_a-z0-9-]+ = one or more of underscore, lowercase, number or dash
  • * = the part dot+name can be present zero or more times
  • @ = the @ sign
  • [a-z0-9-]+ = another name
  • (\.[a-z0-9-]+)* = anoter sequences of zero or more dot+name
  • \. = a dot
  • [a-z]{2,4} = 2 to 4 alphabetic letters
  • $ = end of string

The second one seems instead a bad password validation requiring a sequence of letters and digits with at least a letter and at least a digit.

The (?= ... ) form is called "zero-width look-ahead assertion" and it means that the contained expression must validate but it doesn't "use" characters:

  • ^ = start of string
  • (?=.*[0-9]) = any sequence of chars and a digit (just looking ahead)
  • (?=.*[a-zA-Z]) = any sequence of chars and an alphabetic character (just looking)
  • ([a-zA-Z0-9]+) = one or more chars among numbers and letters
  • $ = end of string

The first two look-ahead assertions are used to check that at least one number and at least one letter are present in the expression that must be composed of just letters and numbers.

6502
  • 112,025
  • 15
  • 165
  • 265
2

(\.[_a-z0-9-]+)* matches the group \.[_a-z0-9-]+ 0 or more times. I assume it's easy to understand what this matches: a dot, followed by alphanumeric characters, dash - or underscore in any order and at least one.

( ... ) is being used to group those characters. * means repeat 0 or more times.

(\.[a-z0-9-]+)* is similar to the above, with the exception that underscores are not allowed here.

(\.[a-z]{2,4})$ will match a dot, followed by 2 to 4 alphabets and ensure that there are no more characters after that.


(?= ... ) in regex is called a positive lookahead. It means that the regex will match only when the regex inside is satisfied.

For instance:

^ checks the start of the string.

(?=.*[0-9]) will make sure the string has at least 1 digit. .*[0-9] will match only when a digit is matched.

(?=.*[a-zA-Z]) will make sure the string has at least 1 letter. .*[a-zA-Z] will match only when a letter is matched.

The advantage of using a lookahead here instead of directly using the character class [a-zA-Z0-9]+ is that it can be used to check the whole string without 'moving'.

The position of the check remains at the start of the string when the string is checked for the digit, and still remains at the start when it is checked for letters.

This in turn allows the regex to ensure that both digit, and letters are present, something that [a-zA-Z0-9]+ alone cannot do since it matches Aaa (no digits) and 123 (no letters) just fine. Using the lookaheads ensure that there are both digits and letters in the match.


Note that not all email addresses will get correctly validated with this regex. There are numerous valid email address format that exist that the regex will tell as invalid. See this question.

Community
  • 1
  • 1
Jerry
  • 70,495
  • 13
  • 100
  • 144
1

Use tool like regexper

  • so first one is email regexp

  • second as @Jerry said is a password, meaning that it has to be alpha numeric string with both letters and digits present

vittore
  • 17,449
  • 6
  • 44
  • 82
  • Ooh, that's cool. I don't understand why anyone would require all of those specific things for a password -_- – James G. Mar 29 '14 at 08:00
0

This question says that the first one is an attempt to check emails using regex, and it seems to check out: Using a regular expression to validate an email address

It seems the second one is a password checker.

This is a good source for information about javascript regex. ?= is a lookahead:

Matches 'x' only if 'x' is followed by 'y'. This is called a lookahead.

For example, /Jack(?=Sprat)/ matches 'Jack' only if it is followed by 'Sprat'. /Jack(?=Sprat|Frost)/ matches 'Jack' only if it is followed by 'Sprat' or 'Frost'. However, neither 'Sprat' nor 'Frost' is part of the match results.

I would also recommend using regexpal, which you can use to quickly test what patterns do or don't match your regex.

Community
  • 1
  • 1
James G.
  • 2,852
  • 3
  • 28
  • 52