🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: pl.comp.lang.javascript
Article #52002

JS i regex

#52002
From: Roman Tyczka
Date: Tue, 12 Feb 2019 14:06
34 lines
1734 bytes
Witam,

Zamarzyło mi się sprawdzać poprawność e-maila, napisałem prosty kod:

...
const reg = new RegExp('^[0-9a-z_.-]+@[0-9a-z.-]+.[a-z]{2,3}$', 'i');
return reg.test(email) == true;	 

i działał, ale wiadomo, że e-maile bywają bardziej skomplikowane, więc
poszukałem "grubszego" regexpa i znalazłem tutaj:

https://emailregex.com/

więc naskrobałem w oparciu o powyższy wzorzec taki kod:

const reg = new RegExp(
      '^(([^<>()[]\\.,;: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,}))$', 'i');
return reg.test(email) == true;

i ... przestało działać, why?

Z kolei taka modyfikacja:

const reg =
/^(([^<>()\[\]\\.,;:\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,}))$/;
return reg.test(String(email).toLowerCase());

znowu działa... czego nie ogarniam?

-- 
pozdrawiam
Roman Tyczka
Technical Details
Message-ID: <11r27xouiiy2h.dlg@tyczka.com>
Path: mbox-mo1.pugleaf.net!news-archive.icm.edu.pl!news.icm.edu.pl!newsfeed2.atman.pl!newsfeed.atman.pl!.POSTED!not-for-mail

Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!news-archive.icm.edu.pl!news.icm.edu.pl!newsfeed2.atman.pl!newsfeed.atman.pl!.POSTED!not-for-mail
From: Roman Tyczka <noemail@because.no>
Newsgroups: pl.comp.lang.javascript
Subject: JS i regex
Date: Tue, 12 Feb 2019 15:06:11 +0100
Organization: ATMAN - ATM S.A.
Lines: 34
Sender: romek@tyczka.no.found
Message-ID: <11r27xouiiy2h.dlg@tyczka.com>
NNTP-Posting-Host: ip-94-75-90-137.wave.com.pl
Mime-Version: 1.0
X-Trace: node2.news.atman.pl 1549980371 30983 94.75.90.137 (12 Feb 2019 14:06:11 GMT)
X-Complaints-To: usenet@atman.pl
NNTP-Posting-Date: Tue, 12 Feb 2019 14:06:11 +0000 (UTC)
User-Agent: 40tude_Dialog/2.0.15.84
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit