JS10 Exercises
Practical
1: Basic Regular Expression Literals
Objective: Write a regular expression that matches specific literal characters in a string.
Tasks:
Create a regular expression to find the word 'cat' in a string. Test the regular expression on a string 'The cat sat on the mat'.
const regex = /cat/;
const testString = 'The cat sat on the mat';
const result = regex.test(testString);
console.log(result); // trueExercise 2: Character Classes and Negation
Objective: Use character classes to match specific sets of characters and their negation.
Tasks:
Write a regular expression to match any digit in the string 'Room number: 123'. Modify the regular expression to match anything except digits in the same string.
// Match any digit
const regexDigits = /\d/;
const testString1 = 'Room number: 123';
console.log(regexDigits.test(testString1)); // true
// Match anything except digits
const regexNonDigits = /\D/;
console.log(regexNonDigits.test(testString1)); // trueExercise 3: Using Wildcards and Quantifiers
Objective: Practice using wildcards and quantifiers in regular expressions.
Tasks:
Create a regular expression that matches any word of any length starting with 'c' and ending with 't'. Test the expression on the string 'cat, cot, carat, cut, concrete'.
const regex = /c\w*t/;
const testString = 'cat, cot, carat, cut, concrete';
const matches = testString.match(regex);
console.log(matches[0]); // catExercise 4: Disjunctions and Assertions
Objective: Implement regular expressions using disjunctions (or) and assertions.
Tasks:
Write a regular expression to match strings that start with 'cat' or end with 'dog'. Test the expression on 'caterpillar', 'dogmatic', and 'concatenate'.
const regex = /^(cat)|dog$/;
console.log(regex.test('caterpillar')); // true
console.log(regex.test('dogmatic')); // false
console.log(regex.test('concatenate')); // trueExercise 5: Using Groups and Backreferences
Objective: Understand how to use capturing groups and backreferences in regular expressions.
Tasks:
Write a regular expression that matches a date in the format 'YYYY-MM-DD' and captures the year, month, and day as separate groups. Use backreferences to rearrange the date into 'DD-MM-YYYY' format.
const regex = /(\d{4})-(\d{2})-(\d{2})/;
const dateString = '2022-01-15';
const newFormat = dateString.replace(regex, '$3-$2-$1');
console.log(newFormat); // 15-01-2022Exercise 6: Regular Expression Methods
Objective: Practice using different RegExp and String methods with regular expressions.
Tasks:
Use RegExp.prototype.test() to check if a string contains 'http' or 'https'. Use String.prototype.matchAll() to find all occurrences of numbers in 'There are 3 apples, 7 bananas, and 12 oranges'. Use String.prototype.replace() to censor swear words in a sentence.
// RegExp.prototype.test()
const httpRegex = /https?/;
console.log(httpRegex.test('http://example.com')); // true
console.log(httpRegex.test('https://example.com')); // true
// String.prototype.matchAll()
const numberRegex = /\d+/g;
const fruitsString = 'There are 3 apples, 7 bananas, and 12 oranges';
const matches = Array.from(fruitsString.matchAll(numberRegex));
console.log(matches.map(match => match[0])); // ['3', '7', '12']
// String.prototype.replace()
const swearWordsRegex = /swearword/gi;
const sentence = 'This is a swearword in a sentence.';
const censored = sentence.replace(swearWordsRegex, '****');
console.log(censored); // This is a **** in a sentence.Exercise 7: Flags in Regular Expressions
Objective: Understand the use of different flags in regular expressions.
Tasks:
Write a regular expression with the global flag to find all instances of 'cat' in a string, regardless of case. Modify the regular expression to use the multiline flag and test it on a multi-line string.
// Global and case-insensitive flags
const regex = /cat/gi;
const multiLineString = 'Cat\nscatter\ncaterpillar';
const matches = Array.from(multiLineString.matchAll(regex));
console.log(matches.map(match => match[0])); // ['Cat', 'cat', 'cat']