0

This is a working regex script in JS:

var emailRegExp   = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/;

How would I escape characters like \ and "

I would like to use it for my model validation like this:

[RegularExpression(@"/^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/")]

But it fails when I escape with either two \ or using @ before the string.

How would this script look like in a C# model validation?

Thanks!

1 Answer 1

1

You need to remove the js delimiter that is the the forward slash present at the start and the end.

@"^(([^<>()[\]\\.,;:\s@""]+(\.[^<>()[\]\\.,;:\s@""]+)*)|("".+""))@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$"
Sign up to request clarification or add additional context in comments.

2 Comments

I would love to accept your answer but it fails with the " character. @"^(([^<>()[]\\.,;:\s@\" <---- here
I had to escape the double quotes aswell like this: @"^(([^<>()[]\\.,;:\s@\"" Please edit your answer so I can accept it :)

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.