-1

So, I want to limit the length of an email address. For example, I want the length of the email address should be less then 20 characters.

let regEx = /[a-zA-Z]([\w]+)@[a-zA-Z]+\.(com)/gim

This is my regex code by which I'm validating an email. I want to limit the length e.g. 20 characters or less. Then I want this regex to match the results from a whole paragraph or strings.

"This first mail address m123456789999@xmail.com shouldn't match because it's too long but this mail m1234@xmail.com should be a match from this whole string."

I tried using (?=.{1,20}$) to limit the length. And it works fine when the mail addresses aren't inside a whole paragraph. For example, it works when

m123456789999@xmail.com //Doesn't match
m1234@xmail.com //Match

But it doesn't match if those emails are inside a whole paragraph. like

"This first mail address m123456789999@xmail.com shouldn't match because it's too long but this mail m1234@xmail.com should be a match from this whole string."

  • Despite the poor email validation capability of the regex presented by the OP, why does the OP not just **test both, the validity and the `length`** of s string like ... `regex.test(email) && email.length <= 20`? Any try of making the length-validation part of the regex makes the regex more complex and/or less reliable in terms of its validation strength. – Peter Seliger Jan 04 '23 at 19:41
  • You want to X. You thought that “limit the length of an email address” would X, but now you discover it won't X. [You need to tell us what X is](https://meta.stackexchange.com/questions/66377/). Why are you messing around with regular expressions for email addresses? [That won't tell you if an email address is valid](https://stackoverflow.com/questions/201323/). Email addresses can be longer than 20 characters. They can contain nonalphabetic characters. If an email address is shorter than 20 characters and is alphabetic, that is no reason to assume it's valid. – Dour High Arch Jan 04 '23 at 20:00
  • To continue on what DourHighArch said - the email addresses also do not have to end in `.com`. Consider `*@yahoo.co.uk`. – FiddlingAway Jan 04 '23 at 22:02
  • The email validation that I wrote to validate was written poorly I know that. My concern is not validating email properly. I was learning regex and I wanted to limit the length of any string with some complex pattern (I've used the email as an example). I was able to limit the length as I've mentioned. But furthermore, I want my regex to match those sequence of pattern which has limited length from anywhere inside a whole paragraph. – mrinmoy saha Jan 05 '23 at 13:43

1 Answers1

1

This will do it:

const input = `This first mail address m123456789999@xmail.com shouldn't match because it's too long but this mail m1234@xmail.com should be a match from this whole string.`;
const regex = /\b(?![\w@\.]{21,})[a-zA-Z]([\w]+)@[a-zA-Z]+\.(com)\b/gim;
const matches = input.match(regex);
console.log(matches);

Output:

[
  "m1234@xmail.com"
]

Explanation of regex:

  • \b -- word boundary to anchor email with non-word char
  • (?![\w@\.]{21,}) -- negative lookahead for 21+ email chars
  • [a-zA-Z]([\w]+)@[a-zA-Z]+\.(com) -- your original regex
  • \b -- word boundary to anchor email with non-word char

Note that your regex is too strict, many additional characters are allowed in email addresses, notably '+' in the name part before @.

Parsing an email address per RFC is actually not that easy, but it can be done reasonably accurate with a shortish regex. An actual RFC:822 compliant e-mail regex is 6000+ characters long!

Here is a shortish regex to validate an email address reasonably accurate:

^\w[\w\.%+-]*@(?:[\w-]+\.)+[a-zA-Z]{2,63}$
\b\w[\w\.%+-]*@(?:[\w-]+\.)+[a-zA-Z]{2,63}\b

The first regex is for a standalone email address, the second one for an email address in a string of text.

Learn more about regex and email address validation: https://twiki.org/cgi-bin/view/Codev/TWikiPresentation2018x10x14Regex

Peter Thoeny
  • 7,379
  • 1
  • 10
  • 20
  • Worth to mention that this regex is probably not RFC 5322 compliant and won't catch all valid emails (or might even match invalid emails). – kelsny Jan 04 '23 at 20:00
  • @vera: Indeed. I updated the answer with a note on RFC:822 compliant e-mail regex – Peter Thoeny Jan 04 '23 at 20:18
  • @PeterThoeny thanks a lot. It worked just the way I wanted. And my concern isn't validating email properly, I was learning regex and was trying to explore its functionality and play with it. Once I master the regex, I will try to write more accurate email validating regex – mrinmoy saha Jan 05 '23 at 14:58
  • 1
    @mrinmoysaha: Glad to help. Care to accept the answer? See big checkmark. – Peter Thoeny Jan 05 '23 at 18:52