Skip to content

Instantly share code, notes, and snippets.

@dwaps
Created November 28, 2019 07:17
Show Gist options
  • Save dwaps/5fcfc7add9fd979b6607782a443a53d7 to your computer and use it in GitHub Desktop.
Save dwaps/5fcfc7add9fd979b6607782a443a53d7 to your computer and use it in GitHub Desktop.
JS: RegExp examples
const mail = "cocojaco@mail.fr";
const regex = new RegExp("^(\\w+)@(\\w+\\.\\w{2,4})$");
const mailOK = regex.test(mail);
const groups = regex.exec(mail);
const localPart = groups[1];
const domainPart = groups[2];
// ES6: email
const [ , local, domain ] = regex.exec(mail);
// ES6: American date format to Fr format
const [ , m, d, y ] = /(\d{2})-(\d{2})-(\d{4})/.exec("11-02-2017");
console.log(`${d}/${m}/${y}`)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment