Skip to content

Instantly share code, notes, and snippets.

@codebubb
Last active April 14, 2023 14:23
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 11 You must be signed in to fork a gist
  • Save codebubb/bb19d7ae24cd41695cb3c5ec3b1155d5 to your computer and use it in GitHub Desktop.
Save codebubb/bb19d7ae24cd41695cb3c5ec3b1155d5 to your computer and use it in GitHub Desktop.
Some Regex string testers
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 james@juniordevelopercentral.com';
// 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.
@uhaq105
Copy link

uhaq105 commented May 2, 2021

dfhdf

@decpk
Copy link

decpk commented May 22, 2021

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 james@juniordevelopercentral.com";

// 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));

@Khosraw
Copy link

Khosraw commented Aug 9, 2021

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 james@juniordevelopercentral.com';
let ex5Regex = /\w+@[\w+.]*/ig;

console.log(ex5.match(ex5Regex).join('').trim());

@RagabMohamedRagab
Copy link

let exRegex1 = /\b\w{3}\b/ig;
let exRegex2 = /[^\d+]/ig;
let exRegex3 = /[\w+ ]
.([\d+.])/ig;
let exRegex4 = /([\d+ ]
)/ig;
let exRegex5 = /([\d+ ]*)/ig;

Copy link

ghost commented Oct 28, 2021

// 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 james@juniordevelopercentral.com';
let reg5 = /(\w+)@(\w+)\.(\w{3})/gi;
console.log(ex5.match(reg5))

@Mohammad-Echaary
Copy link

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 james@juniordevelopercentral.com';
console.log(ex5.match(/\w+@\w+.\w+/g))

@TheRealSmeddy
Copy link

// 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);

@Flori1234657
Copy link

//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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment