0

I am using a regular expression to validate an email address with example@gmail.com or example@google.net.

This is my regex:

/^([A-za-z0-9_\.-]+\@[\gmail\.-]+\.[com\.]{2,6})$/  

However, it is wrong, somebody help me, thanks very much.

jkdev
  • 11,360
  • 15
  • 54
  • 77
Kaka Nghia
  • 19
  • 2

2 Answers2

0

This regex will match both xxx@gmail.com and xxx@google.net

^[A-Za-z0-9_.\-]+@(?:gmail\.com|google\.net)$

You'll notice that the 2 domains are put in a non-capturing group, separated by a pipe (OR)

Do note also that something like [com] in a regex is not to match the word "com", but to match the characters "c","o","m".

LukStorms
  • 28,916
  • 5
  • 31
  • 45
  • 1
    [`[A-z]` matches more than just letters](http://stackoverflow.com/questions/29771901/why-is-this-regex-allowing-a-caret/29771926#29771926). – Wiktor Stribiżew Sep 16 '16 at 07:52
0

Want something like

\w*[gmail]\.(?!com)
Omari Victor Omosa
  • 2,814
  • 2
  • 24
  • 46