Day 19: Regular Expressions
Screenshot:-
Regular expressions (regex) are patterns used to match character combinations in strings. They are powerful tools for searching, replacing, and validating text.
Tasks/Activities:
Activity 1: Basic Regular Expressions
Task 1: Match a Simple Pattern Write a regex to match all occurrences of the word "JavaScript" in a string.
const text = "JavaScript is awesome. I love JavaScript!";
const regex = /JavaScript/g; // 'g' flag for global match
const matches = text.match(regex);
console.log(matches); // Output: [ 'JavaScript', 'JavaScript' ]
Task 2: Match All Digits Write a regex to match all digits in a string.
const text = "My phone number is 123-456-7890";
const regex = /\d+/g; // '\d' matches digits, '+' for one or more digits
const matches = text.match(regex);
console.log(matches); // Output: [ '123', '456', '7890' ]
Activity 2: Character Classes and Quantifiers
Task 3: Match Words Starting with a Capital Letter Write a regex to match all words that start with a capital letter.
const text = "Hello World. Welcome to JavaScript.";
const regex = /\b[A-Z][a-z]*\b/g; // '\b' for word boundary, '[A-Z]' for capital letter, '[a-z]*' for lowercase letters
const matches = text.match(regex);
console.log(matches); // Output: [ 'Hello', 'World', 'Welcome', 'JavaScript' ]
Task 4: Match Sequences of Digits Write a regex to match all sequences of one or more digits.
const text = "There are 123 apples, 45 oranges, and 6 bananas.";
const regex = /\d+/g; // '\d+' for one or more digits
const matches = text.match(regex);
console.log(matches); // Output: [ '123', '45', '6' ]
Activity 3: Grouping and Capturing
Task 5: Capture US Phone Number Parts Write a regex to capture the area code, central office code, and line number from a US phone number format.
const phone = "(123) 456-7890";
const regex = /\((\d{3})\) (\d{3})-(\d{4})/; // '\d{3}' for 3 digits, capturing groups for parts
const matches = phone.match(regex);
console.log(matches); // Output: [ '(123) 456-7890', '123', '456', '7890' ]
Task 6: Capture Email Username and Domain Write a regex to capture the username and domain from an email address.
const email = "user@example.com";
const regex = /(\w+)@(\w+\.\w+)/; // '\w+' for one or more word characters, capturing groups for username and domain
const matches = email.match(regex);
console.log(matches); // Output: [ 'user@example.com', 'user', 'example.com' ]
Activity 4: Assertions and Boundaries
Task 7: Match Word at Beginning of String Write a regex to match a word only if it is at the beginning of a string.
const text = "Hello world. Hello JavaScript.";
const regex = /^Hello/; // '^' asserts position at start of a line
const matches = text.match(regex);
console.log(matches); // Output: [ 'Hello' ]
Task 8: Match Word at End of String Write a regex to match a word only if it is at the end of a string.
const text = "Hello world. Welcome to JavaScript.";
const regex = /JavaScript\.$/; // '$' asserts position at end of a line
const matches = text.match(regex);
console.log(matches); // Output: [ 'JavaScript.' ]
Activity 5: Practical Applications
Task 9: Validate Password Write a regex to validate a password (must include at least one uppercase letter, one lowercase letter, one digit, and one special character).
const password = "Passw0rd!";
const regex = /^(?=.*[A-Z])(?=.*[a-z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{8,}$/; // '(?=.*[A-Z])' for uppercase, '(?=.*[a-z])' for lowercase, '(?=.*\d)' for digit, '(?=.*[@$!%*?&])' for special character, '{8,}' for minimum length 8
const isValid = regex.test(password);
console.log(isValid); // Output: true
Task 10: Validate URL Write a regex to validate a URL.
const url = "https://www.example.com";
const regex = /^(https?:\/\/)?(www\.)?[\w-]+\.[a-z]{2,6}(\/[\w-]*)*\/?$/; // 'https?:\/\/' for http/https, '(www\.)?' for optional www, '[\w-]+\.[a-z]{2,6}' for domain, '(\/[\w-]*)*\/?' for optional path
const isValid = regex.test(url);
console.log(isValid); // Output: true
Regular expressions can seem tricky at first, but with practice, they become powerful tools in your coding toolkit!