Regular Expression (RegExp) - Validating Email Addresses

Hey there,

Short question, I tried the following regexp in different places before deciding to use it in the Editor’s IDE, and it works flawlessly, but the IDE is giving a warning, does it need to be declared differently?

BTW, this regexp is to validate email addresses.

/^(?:[a-z0-9!#$%&'+/=?^_{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_{|}~-]+)|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\[\x01-\x09\x0b\x0c\x0e-\x7f])")@(?:(?:a-z0-9?.)+a-z0-9?|[(?:(?:(2(5[0-5]|[0-4][0-9])|1[0-9][0-9]|[1-9]?[0-9])).){3}(?:(2(5[0-5]|[0-4][0-9])|1[0-9][0-9]|[1-9]?[0-9])|[a-z0-9-][a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\[\x01-\x09\x0b\x0c\x0e-\x7f])+)])$/

const regex = /^(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:(2(5[0-5]|[0-4][0-9])|1[0-9][0-9]|[1-9]?[0-9]))\.){3}(?:(2(5[0-5]|[0-4][0-9])|1[0-9][0-9]|[1-9]?[0-9])|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])$/

regex.test('contact@nasriya.net');

The warning is “Unexpected control character(s) in regular expression”.

Edit: The actual problem is that using this regex makes everything after it a string.

Any ideas?

1 Like

Try to delete the /^ from the beginning and the $/ from the end. Worked for me. We found the same one.

EDIT : they differ slightly. Mine is:
(?:[a-z0-9!#$%&'+/=?^_{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_{|}~-]+)|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\[\x01-\x09\x0b\x0c\x0e-\x7f])")@(?:(?:a-z0-9?.)+a-z0-9?|[(?:(?:(2(5[0-5]|[0-4][0-9])|1[0-9][0-9]|[1-9]?[0-9])).){3}(?:(2(5[0-5]|[0-4][0-9])|1[0-9][0-9]|[1-9]?[0-9])|[a-z0-9-][a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\[\x01-\x09\x0b\x0c\x0e-\x7f])+)])

Are you using the input regex or you are declaring it in code?

@ahmadnasriya Input

@giri-zano Ah, I’m validating the email address on the backend, the email is passed as a parameter of an HTTP post request.

Uh, you do realize that regex gives me a major headache? Makes my eyes roll around in their sockets.

It’s only a warning, but if it works, then what could be the issue? And when I tried @giri-zano 's, I get errors. Giri, did you try yours in code? Do you get errors? Does it work?

Could the problem be the regex “flavor”? I know that there are some nuances between regexes for Javascript, PHP, Java, etc. Drives me nuts.

I’ll bring this to the attention of the appropriate team and see what they say. Thanks!

There is a linter. ESLint is checked that your code doesn’t include “bad” practice. It’s not an error, it’s just a warning that you shouldn’t use characters in the ASCII in RegExp.

Here:
https://eslint.org/docs/rules/no-control-regex#disallow-control-characters-in-regular-expressions-no-control-regex

If you trust this RegExp then you can off the warning with command comment

/* eslint no-control-regex: "off" */
const regex = /^(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:(2(5[0-5]|[0-4][0-9])|1[0-9][0-9]|[1-9]?[0-9]))\.){3}(?:(2(5[0-5]|[0-4][0-9])|1[0-9][0-9]|[1-9]?[0-9])|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])$/

Well, then we should make sure to keep them away from you, or get you a variety of flavors to choose from, you might find one that you like :grin:

I tried it on JavaScript based applications, and it worked.
Thank you.

Thank you for your answer.

I already know that, but the problem isn’t in the warning itself, or I’d have just ignored it, the problem is that the IDE considered the first few characters as a regex and the rest as a regular text, making all the code after it as a text.

Lines 3 and 4 are JS code, but adding the regex making them regular texts.

Thanks

@ahmadnasriya Interesting - I tried and didn’t see that. Hmmm

@yisrael-wix with the Editor’s IDE? I know that it works on other JS based IDEs.

@ahmadnasriya Damn! Just tried again and you’re right. Maybe I did it differently this time.

Removing the 18th character, which is the ( ) fixes the weird string thing. But I’m not sure if the regex still works as before, or if it lost some of its validation conditions. :man_shrugging:

Does anybody ever know if a regex really works?

The RegEx is valid. Please, return back the char ) It’s looks like as a bug in the editor parce for code highlighting.

@yisrael-wix :grin:

You can move this RegExp to public file and export it.

import { regexp } from 'public/regexp.js';

regex.test('contact@nasriya.net');

@yisrael-wix yes both of them work, however, I only ran a few emails of the modified version.

@alexanderz61239 I don’t think the public files are any different, but I tried it anyway, and the issue persisted.

@ahmadnasriya Reading it from a text column inside a collection, would that work?