0

Thank you in advance for your help.

I am trying to validate the email address entered with regex pattern i.e. "pattern": [a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,4}$ which is working as expected but I would like this pattern also check for apostrophe i.e. '

I have seen so many examples suggested by devs in stackoverflow but nothing helped. Could you please help me to resolve it.

Example I tried so far is :

"pattern": [a-zA-Z0-9._%+-']+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,4}$

"pattern": [a-zA-Z0-9._%+-\']+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,4}$

"pattern": ['][a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,4}$

I am getting error as its inside double quotes saying invalid character

Christian Baumann
  • 3,188
  • 3
  • 20
  • 37
Adithya
  • 183
  • 1
  • 2
  • 16
  • Have you tried to define your pattern using `/pattern/` syntax? – Alexander Mashin Sep 30 '20 at 09:06
  • @AlexanderMashin : Thank you for your quick response. I tried but I am getting error saying incorrect syntax near /. "pattern": "[a-zA-Z0-9._%+-//']+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,4}$", – Adithya Sep 30 '20 at 09:11
  • @AlexanderMashin : Also I tried placing the ' in-between the forward slash too, but I am getting error "pattern": "[a-zA-Z0-9._%+-//'//]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,4}$", – Adithya Sep 30 '20 at 09:15
  • 1
    Between forward slashes, you do not escape apostrophes and backslashes, but escape forward slashes: `/` → `\/`. – Alexander Mashin Sep 30 '20 at 09:18
  • @AlexanderMashin : Thanks again for correcting me with your explanation with slashes preferences, but I am still struck with fixing the issue, as I am not worked on regex earlier, could you please help me with a solution pattern for this time, I will try to learn regex patterns soon:). – Adithya Sep 30 '20 at 09:26
  • 1
    You need to either escape `-` or put at the end when it is in a character class.See [your regex demo](https://regex101.com/r/XXaUVi/1) – Wiktor Stribiżew Sep 30 '20 at 10:05
  • The problem is solved, thank you @alexander,Wiktor for your valueble inputs, issue is been resolved, to allow apostrophe' in email here is the pattern "[a-zA-Z0-9.''_%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,4}$" – Adithya Sep 30 '20 at 15:45

1 Answers1

1
let pattern = /^(?<user>[a-zA-Z0-9._%+'-]+)@(?<domain>[a-zA-Z0-9.'-]+\.[a-zA-Z]{2,4})$/;

Apostrophe added, unnecessary escapings removed.

See https://regex101.com/r/QUonyq/1.

Although, your regular expression is not the one that validates email addresses. See https://stackoverflow.com/a/201378/6632736 for the real one.

Alexander Mashin
  • 3,892
  • 1
  • 9
  • 15
  • Thank you for the help but my problem is I am depending on angular formly module where within template options with pattern as key I need to give value as "pattern": "[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,4}$" – Adithya Sep 30 '20 at 12:56
  • The flow is like this, the application UI will be rendered based on angular formly configuration such as label input type(text,select etc) and css which is configured in json with array and objects and the same will be retired form db and rendered in UI – Adithya Sep 30 '20 at 12:58
  • You mean, you need a `String`, not `RegExp`? Then try `"^(?[a-zA-Z0-9._%+'-]+)@(?[a-zA-Z0-9.'-]+\.[a-zA-Z]{2,4})$"`. Note that `-` goes last in character classes. JSON uses double quotes. – Alexander Mashin Sep 30 '20 at 13:06
  • hey yes but the problem is I extracted this from ur solution where I removed user,domain and this is the final regex I am trying "^[a-zA-Z0-9._%+'-]+@([a-zA-Z0-9.'-]+\.[a-zA-Z]{2,4}$" where the apostrophe ' is still thorwing error right after its next character(incorrect syntax near'-') – Adithya Sep 30 '20 at 13:13
  • I am debugging more, please ignore my question for now, I will be posting my answer very soon, I am glad to have your inputs which helped me to dig more, thank you so much for correcting me and baring my questions related to regex, where I am pretty poor. – Adithya Sep 30 '20 at 14:34
  • The problem is solved, thank you @alexander,Wiktor for your valueble inputs, issue is been resolved, to allow apostrophe' in email here is the pattern "[a-zA-Z0-9.''_%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,4}$" – Adithya Sep 30 '20 at 15:45