Home > Software engineering >  Is there any way to limit the length of a string by regular expression and match them from a whole p
Is there any way to limit the length of a string by regular expression and match them from a whole p

Time:01-06

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 [email protected] shouldn't match because it's too long but this mail [email protected] 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

[email protected] //Doesn't match
[email protected] //Match

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

"This first mail address [email protected] shouldn't match because it's too long but this mail [email protected] should be a match from this whole string."

CodePudding user response:

This will do it:

const input = `This first mail address [email protected] shouldn't match because it's too long but this mail [email protected] 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:

[
  "[email protected]"
]

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

  • Related