Skip to content

Instantly share code, notes, and snippets.

@cicerohen
Last active February 25, 2022 22:41
Show Gist options
  • Save cicerohen/f2fff91abc33382210cba2331bdfc65a to your computer and use it in GitHub Desktop.
Save cicerohen/f2fff91abc33382210cba2331bdfc65a to your computer and use it in GitHub Desktop.
Get Started With Regex
/*
GET STARTED WITH REGULAR EXPRESSIONS
1. Main Concepts
1.1. Capture Groups
1.2. Quantifiers and Alternation
1.3. Anchors
1.4. Character Classes
1.5. Word Boundary
1.6. Flags
1.6.1. Ignore Case
2. Helper Methods
2.1. String.match()
2.2. Regex.test()
2.3. String.replace()
3. Examples
*/
/*
3.1 Test and extract email patterns from a string
Valid email patterns:
cicero@gmail.com
cicero@gmail.com.br
cicero.viana@gmail.com.br
cicero_h-r.viana@gmail.com.br
cicero123@gmail.com.br
Email regex:
*/
const emailRE = /^[a-z0-9\.\-_]+@{1}[a-z]+.com+\.[\w]{0,2}/;
const emailRE_2 = /^[a-z0-9\.\-_]+@{1}[a-z]+.com(\.\w{2})?$/;
/*
3.2 Test and extract URL patterns from a string
Valid URL patterns
http://www.google.com;
https://google.com;
http://google.com?foo=1&bar=1;
https://www.images.google.com?foo=1&bar=1;
URL regex
*/
const urlRE = /http[s]?:\/\/www.\w*\.\w{3}/;
const urlRE_2 = /(http[s]?:\/\/)(\w*)((?:\.[\w]*)+)((?:[?&]\w+=\w+)*)/;
/*
3.3. Test and extract phone number patterns from a string
Valid phone number patterns
(85)98765-4321;
(85)9875-4321;
85 98765-4321;
+55 (85) 98765-4321;
+55 85 98765-4321;
*/
/*
Phone regex
*/
const phoneRE = /^\(\d{2}\)\w{5}-\d{4}/;
const phoneRE_2 = /^\(\d{2}\)\w{4,5}-\d{4}/;
const phoneRE_3 = /^(\+\d{2}\s)?\(?\d{2}\)?\s?\d{4,5}-\d{4}/;
/*
4. References
4.1. https://www.regular-expressions.info/tutorial.html
4.2. https://regexr.com/
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment