RegExp in JS

Basic

/pattern/flags

  • two forward slashes to show start and end of the pattern;
  • flags(optional): to indicate how to search (i,g,m).

Syntax

Tasks

Email

const emailRegExp = /[a-zA-z0-9_.]+@[a-z]+\.\w+/;

emailRegExp.test('andri_ivihor@icloud.com'); // true
emailRegExp.test('@icloud.com');             // false
emailRegExp.test('andri_ivihor@icloud.com'); // true

Phone

const phoneRegExp = /\+\d+\(\d+\)[ ,-]?\d+[ ,-]?\d+[ ,-]?\d+/;

phoneRegExp.test('+380(66)5643290'); //true
phoneRegExp.test('380(66)5643290'); //false
phoneRegExp.test('+380(66) 56 43 290') //true

Password

const passwordRegExp = 
      /(?=.*[\d])(?=.*[A-Z])(?=.*[a-z])(?=.*[!@#$%^&*])[\w!@#$%^&*]{6,}/;

passwordRegExp.test('Iwas123$'); //true
passwordRegExp.test('Iwas123'); //false

Date

const dateRegExp =
 /(0[1-9]|1[0-2])[ /-]?(0[1-9]|[12]\d|3[01])[ /-]?[012]\d{3}
|(0[1-9]|[12]\d|3[01])[ /-]?(0[1-9]|[12]\d|3[01])[ /-]?[012]\d{3}/;

dateRegExp.test('11/30/2018'); //true 
dateRegExp.test('31-02-2018'); //true
dateRegExp.test('21-10-201');  //false

One
More
Thing...

deck

By just Just

deck

  • 51