Regular Expressions (RegEx) Flashcards

Cards and exercises.Find a recognized patterns in string or text. useful resources: https://regexone.com/ https://www.rexegg.com/ https://www.w3resource.com/javascript-exercises/javascript-regexp-exercises.php https://regex.sketchengine.co.uk/

1
Q

Use capture groups in reRegex to match numbers that are repeated only three times in a string, each separated by a space.

I.e. match “10 10 10”, “42 42 42” and so on

A

Possible Solution:

let repeatNum = "42 42 42";
let reRegex =  /^(\d+)\s\1\s\1$/;
let result = reRegex.test(repeatNum);
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Regular Expressions: Use Capture Groups to Search and Replace

Write a regex so that it will search for the string “good”. Then update the replaceText variable to replace “good” with “okey-dokey”.

A

Possible Solution:

let huhText = "This sandwich is good.";
let fixRegex = /good/; // Change this line
let replaceText = "okey-dokey"; // Change this line
let result = huhText.replace(fixRegex, replaceText);
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Regular Expressions: Remove Whitespace from Start and End
Sometimes whitespace characters around strings are not wanted but are there. Typical processing of strings is to remove the whitespace at the start and end of it.

Write a regex and use the appropriate string methods to remove whitespace at the beginning and end of strings.

Note
The .trim() method would work here, but you’ll need to complete this challenge using regular expressions.

A

Possible Solution:

let hello = “ Hello, World! “;
let wsRegex = /^\s+|\s+$/g; // Change this line
let result = hello.replace(wsRegex, ‘’); // Change this line
let match = hello.match(wsRegex);
console.log(match);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly