0

On my frontend, I have a regex in javascript to detect if the email was correct.

^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$

Current backend Regex validation, on .NET is:

Regex.IsMatch(email, @"\A(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?)\Z", RegexOptions.IgnoreCase);

I want to replace the .NET regex by the javascript one But I'm really "lost" in the begin/end characters.

In the .NET regex, have \A and \Z the same utility than ^ and $ in my javascript one?

I tried to replace my .NET regex as follows, but it doesn't detect valid string even if in Javascript it was correct:

Regex.IsMatch(email, @"\A(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))\Z", RegexOptions.IgnoreCase);
5
  • No duplicate about Email Validation: my question is not about the correct regex to use, but about the begind/end symbol difference between Javascript and C# Commented Jan 19, 2017 at 18:36
  • Have you seen the MSDN reference? And Anchors in Regular Expressions. Commented Jan 19, 2017 at 18:37
  • Thanks Jasen for reference. I tried to keep A and Z but it doesn't validate the string correctly. Commented Jan 19, 2017 at 18:44
  • Well, now you have a second question. You should create a new post with example data. Commented Jan 19, 2017 at 18:47
  • The answer given is wrong, why did you accept it? Vladu just pasted the description of the anchors from the .NET reference, in JS, the \A and \Z are not supported and $ does not behave the same. Commented Jan 6, 2022 at 17:19

1 Answer 1

2
^ -   The match must occur at the beginning of the string or line. 
$ -   The match must occur at the end of the string or line, or before \n at the end of the string or line.
\A -  The match must occur at the beginning of the string only (no multiline support).
\Z -  The match must occur at the end of the string, or before \n at the end of the string.
Sign up to request clarification or add additional context in comments.

2 Comments

So the difference is to support or not multiline?
I edited the question: i tried to keep the A and Z anchors but it doesn't validate the string as it is done in javascript

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.