Last active
February 25, 2022 22:41
-
-
Save cicerohen/f2fff91abc33382210cba2331bdfc65a to your computer and use it in GitHub Desktop.
Get Started With Regex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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: | |
[email protected] | |
[email protected] | |
[email protected] | |
[email protected] | |
[email protected] | |
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