-
-
Save codebubb/bb19d7ae24cd41695cb3c5ec3b1155d5 to your computer and use it in GitHub Desktop.
const ex1 = 'The quick brown fox jumped over the lazy dog'; | |
const ex2 = 'A1B2C3D4E5F6G7H8I9J10'; | |
const ex3 = 'The salad costs $9.99'; | |
const ex4 = 'Contact customer support on 0800 300 500'; | |
const ex5 = 'You can contact me on Twitter @codebubb or [email protected]'; | |
// Exercise 01 | |
// Using a regex pattern, get the 3 letter words in the ex1 string. | |
// Exercise 02 | |
// Using a regex pattern, remove all of the numbers from the ex2 string. | |
// Exercise 03 | |
// Using a regex pattern, find the monetary value contained within the ex3 string. | |
// Exercise 04 | |
// Using a regex pattern, find the telephone number contained within the ex4 string. | |
// Exercise 05 | |
// Using a regex pattern, find the email address contained within the ex5 string. | |
const ex1 = "The quick brown fox jumped over the lazy dog";
const ex2 = "A1B2C3D4E5F6G7H8I9J10";
const ex3 = "The salad costs $9.99";
const ex4 = "Contact customer support on 0800 300 500";
const ex5 =
"You can contact me on Twitter @codebubb or [email protected]";
// Exercise 01
// Using a regex pattern, get the 3 letter words in the ex1 string.
console.log(ex1.match(/\b\w{3}\b/g));
// Exercise 02
// Using a regex pattern, remove all of the numbers from the ex2 string.
console.log(ex2.replaceAll(/\d+/g, ""));
// Exercise 03
// Using a regex pattern, find the monetary value contained within the ex3 string.
console.log(ex3.match(/(?<=\$)\d+\.\d+/g));
// Exercise 04
// Using a regex pattern, find the telephone number contained within the ex4 string.
console.log(ex4.match(/\d{4}\s\d{3}\s\d{3}/g));
// Exercise 05
// Using a regex pattern, find the email address contained within the ex5 string.
console.log(ex5.match(/\w+@\w+.com/g));
Nice stuff!
// EX1
const ex1 = 'The quick brown fox jumped over the lazy dog';
let ex1Regex = /\b\w{3}\b/ig;
console.log(ex1.match(ex1Regex));
// EX2
const ex2 = 'A1B2C3D4E5F6G7H8I9J10';
let ex2Regex = /[^\d+]*/ig;
console.log(ex2.match(ex2Regex).join(''));
// EX3
const ex3 = "The salad costs $9.99";
let ex3Regex = /[\w+ ]*\.([\d+.]*)/ig;
console.log(ex3.match(ex3Regex).join(''));
// EX4
const ex4 = 'Contact customer support on 0800 300 500';
let ex4Regex = /([\d+ ]*)/ig;
console.log(ex4.match(ex4Regex).join('').trim());
// EX5
const ex5 = 'You can contact me on Twitter @codebubb or [email protected]';
let ex5Regex = /\w+@[\w+.]*/ig;
console.log(ex5.match(ex5Regex).join('').trim());
let exRegex1 = /\b\w{3}\b/ig;
let exRegex2 = /[^\d+]/ig;
let exRegex3 = /[\w+ ].([\d+.])/ig;
let exRegex4 = /([\d+ ])/ig;
let exRegex5 = /([\d+ ]*)/ig;
// Exercise 01
// Using a regex pattern, get the 3 letter words in the ex1 string.
const ex1 = 'The quick brown fox jumped over the lazy dog';
console.log(ex1.match(/\b([\w]{3})\b)/gi)
// Exercise 02
// Using a regex pattern, remove all of the numbers from the ex2 string.
const ex2 = 'A1B2C3D4E5F6G7H8I9J10';
let replace = '/\d+/gi';
console.log(ex2.replace(replace, ""))
// Exercise 03
// Using a regex pattern, find the monetary value contained within the ex3 string.
const ex3 = 'The salad costs $9.99';
console.log(ex3.match(/\B\$(\d+)\.(\d+)/gi))
// Exercise 04
// Using a regex pattern, find the telephone number contained within the ex4 string.
const ex4 = 'Contact customer support on 0800 300 500';
let number = /([\d]{4})\s([\d]{3})\s([\d]{3})$/gi;
console.log(ex4.match(number));
// Exercise 05
// Using a regex pattern, find the email address contained within the ex5 string.
const ex5 = 'You can contact me on Twitter @hozayves or [email protected]';
let reg5 = /(\w+)@(\w+)\.(\w{3})/gi;
console.log(ex5.match(reg5))
const ex1 = 'The quick brown fox jumped over the lazy dog';
console.log( ex1.match(/[A-Z]{3}/ig))
const ex2 = 'A1B2C3D4E5F6G7H8I9J10';
console.log(ex2.match(/[^0-9]/g).join(""))
const ex3 = 'The salad costs $34859.99';
console.log(ex3.match(/(?<= $)\d+.\d+/g))
const ex4 = 'Contact customer support on 0800 300 500';
console.log(ex4.match(/[0-9]+/g).join(" "))
const ex5 = 'You can contact me on Twitter @codebubb or [email protected]';
console.log(ex5.match(/\w+@\w+.\w+/g))
// Exercise 01
// Using a regex pattern, get the 3 letter words in the ex1 string.
ex1.math(/\b\w{3}\b/g);
// Exercise 02
// Using a regex pattern, remove all of the numbers from the ex2 string.
ex2.replaceAll(/\d/g, "");
// Exercise 03
// Using a regex pattern, find the monetary value contained within the ex3 string.
ex3.match(/\d+.\d+(?!$)/g);
// Exercise 04
// Using a regex pattern, find the telephone number contained within the ex4 string.
ex4.match(/\d{4}\s\d{3}\s\d{3}/g);
// Exercise 05
// Using a regex pattern, find the email address contained within the ex5 string.
ex5.match(/[\w\d]+@\w+.\w{2,5}/g);
//EX-1
console.log(ex1.match(/^\w+/ig)
)
//EX-2
console.log(ex2.match(/\D/ig)
)
//EX-3
console.log(ex3.match(/\W\d[\W]\d+/g)
)
//EX-4
console.log(ex4.match(/\d/g)
)
//EX-5
console.log(ex5.match(/@[a-z]+/g)
) Until emails start with @ you don't need a big logic to do that
dfhdf