Skip to content

Commit 4f94b38

Browse files
committed
Add code for workshop
1 parent 1de6592 commit 4f94b38

16 files changed

+1010
-347
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# jsnation-regex-workshop
2+
3+
Code for the [JSNation Live](https://live.jsnation.com/workshops-3h) Regular Expressions workshop, June 14, 2021

exercises/1-regexp-methods.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
const assert = require("assert");
2+
3+
// Exercise 1 ///////////////////////////////////////////////////////////////////////
4+
// Return true if the input string contains a word starting with
5+
// q (case insensitive), false otherwise
6+
const hasQWord = (inputString) => {
7+
const regex = /\bq\w+\b/i;
8+
// write function here
9+
};
10+
assert(hasQWord("I adore quails.") === true);
11+
assert(hasQWord("Quinn left her job! No way!") === true);
12+
assert(hasQWord("This exquisite string has no matching words.") === false);
13+
14+
// Exercise 2 ///////////////////////////////////////////////////////////////////////
15+
// Return the first full word of the input string. Could be preceded or
16+
// followed by a space, newline, punctuation, etc.
17+
const findFirstWord = (inputString) => {
18+
const regex = /^\W*\w+\b/;
19+
// write funtion here
20+
};
21+
assert(
22+
findFirstWord("I, a JavaScript fan, am at the JSNation regex workshop!"),
23+
"I"
24+
);
25+
assert(findFirstWord("\nWhat is that newline doing there?\n"), "What");

exercises/2-string-methods.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
const assert = require("assert");
2+
3+
// Exercise 1 ///////////////////////////////////////////////////////////////////////
4+
// Return the punchline of the joke, assuming the punchline starts after a ? and any
5+
// number of space characters, using a string method.
6+
// Note: later we will do this same exercise using string splitting.
7+
const getPunchline = (joke) => {
8+
const regex = /[^\?]\?\s*/;
9+
// write function here
10+
};
11+
12+
const joke1 =
13+
"What's the difference between Java and JavaScript? Java is like JavaScript the way car is like carpet.";
14+
const punchline1 = "Java is like JavaScript the way car is like carpet.";
15+
assert.strictEqual(getPunchline(joke1), punchline1);
16+
17+
const joke2 =
18+
"Why do Javascript developers prefer dark mode? Because light attracts bugs.";
19+
const punchline2 = "Because light attracts bugs.";
20+
assert.strictEqual(getPunchline(joke2), punchline2);
21+
22+
// Exercise 2 ///////////////////////////////////////////////////////////////////////
23+
// Return an array of words in the input string that start with the letter j (case
24+
// insensitive) and end with the letter t (also case insensitive).
25+
const getJTwords = (inputString) => {
26+
const regex = /\bj\w+t\b/gi;
27+
// write function here
28+
};
29+
30+
const JTstring1 = "JavaScript just makes me jump for joy.";
31+
assert.deepStrictEqual(getJTwords(JTstring1), ["JavaScript", "just"]);
32+
33+
const JTstring2 = "James and Justine went for a walk";
34+
assert.deepStrictEqual(getJTwords(JTstring2), null);
35+
36+
// Exercise 3 ///////////////////////////////////////////////////////////////////////
37+
// Find all of the amounts in USD and return them in an array (hint: use capture groups)
38+
const getUSDamounts = (string) => {
39+
const regex = /^USD: \$([\d\.]+)/gm;
40+
// write function here
41+
};
42+
43+
const currencyString = `
44+
AUD: $6.09
45+
USD: $5.70
46+
JPY: ¥117.84
47+
EUR: €36.12
48+
USD: $12.89
49+
`;
50+
assert.deepStrictEqual(getUSDamounts(currencyString), ["5.70", "12.89"]);

exercises/3-capture-groups.js

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
const assert = require("assert");
2+
3+
// EXERCISE 1 ///////////////////////////////////////////////////////////////////////
4+
// Return true if string has the first word repeated in the string (case insensitive),
5+
// false otherwise
6+
const stringRepeatsFirstWord = (inputString) => {
7+
// note, you will have to adjust this regex so that it finds strings that start
8+
// and end with the same word
9+
// const regexToFindFirstWord = /^(\b\w+\b)/i;
10+
//
11+
// write function here
12+
};
13+
14+
const stringWithBookends = "Which witch is which?";
15+
assert(stringRepeatsFirstWord(stringWithBookends) === true);
16+
17+
const stringWithRepeats = "To know her is to love her";
18+
assert(stringRepeatsFirstWord(stringWithRepeats) === true);
19+
20+
const averageString = "This is just your average string.";
21+
assert(stringRepeatsFirstWord(averageString) === false);
22+
23+
// EXERCISE 2 ///////////////////////////////////////////////////////////////////////
24+
// Given data shaped like this:
25+
// 28may2021 04:55:38 This is a message
26+
// 03jun2021 23:44:01 This is another message
27+
// Extract the month and day as a group named “date”, the year into a group named "year",
28+
// the time as a group named “time” and the message as a group named “message”
29+
30+
const parseLineIntoObj = (logLine) => {
31+
// you will have to separate this regex into named groups!
32+
const regex = /^\d{2}[a-z]{3}\d{4} \d{2}:\d{2}:\d{2} .+$/;
33+
34+
// write the function here
35+
};
36+
37+
const logLine1 = "28may2021 04:55:38 Something happened here";
38+
const result1 = {
39+
date: "28may",
40+
year: "2021",
41+
time: "04:55:38",
42+
message: "Something happened here",
43+
};
44+
assert.deepStrictEqual(parseLineIntoObj(logLine1), result1);
45+
46+
const logLine2 = "03jun2021 23:44:01 Nothing happened here";
47+
const result2 = {
48+
date: "03jun",
49+
year: "2021",
50+
time: "23:44:01",
51+
message: "Nothing happened here",
52+
};
53+
assert.deepStrictEqual(parseLineIntoObj(logLine2), result2);

exercises/4-split-replace.js

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
const assert = require("assert");
2+
3+
// Exercise 1 ///////////////////////////////////////////////////////////////////////
4+
// EXAMPLE: Replace any email address (using the simplified regex below)
5+
// with the text "<<redacted>>"
6+
const redactEmails = (text) => {
7+
const regex = /\b[\w\.]+@[\w\.]+\.[\w]+\b/g;
8+
9+
// write function here
10+
};
11+
12+
const emailString1 = "[email protected]";
13+
assert.strictEqual(redactEmails(emailString1), "<<redacted>>");
14+
15+
const emailString2 =
16+
17+
const redactedString2 =
18+
"Please send to <<redacted>>, <<redacted>> and <<redacted>>. Thanks!";
19+
assert.strictEqual(redactEmails(emailString2), redactedString2);
20+
21+
const emailString3 = "This string has no emails!!";
22+
assert.strictEqual(redactEmails(emailString3), emailString3);
23+
24+
// Exercise 2 ///////////////////////////////////////////////////////////////////////
25+
// EXAMPLE: Strip off any whitespace characters from the beginning or end of a string,
26+
// but let the spaces in the middle stand.
27+
const stripWhitespace = (inputString) => {
28+
// you'll have to add groups in the following regex to make this one work
29+
const regex = /^\s*.*\s*$/;
30+
31+
// write function here
32+
};
33+
34+
const input1 = " that's some unnecessary whitespace \n\n";
35+
const output1 = "that's some unnecessary whitespace";
36+
assert.deepEqual(stripWhitespace(input1), output1);
37+
38+
const input2 = '\n"Get to the back of the ship!" Tom said sternly.\n';
39+
const output2 = '"Get to the back of the ship!" Tom said sternly.';
40+
assert.deepEqual(stripWhitespace(input2), output2);
41+
42+
const noOuterSpaceString = "Do not launch me out of the atmosphere please!";
43+
assert.deepEqual(stripWhitespace(noOuterSpaceString), noOuterSpaceString);
44+
45+
// Exercise 3 ///////////////////////////////////////////////////////////////////////
46+
// Remember this one? Return the punchline of the joke, assuming the punchline starts
47+
// after a ? and any number of space characters, this time using string splitting.
48+
const getPunchline = (joke) => {
49+
const regex = /\?\s*/;
50+
51+
// write function here
52+
};
53+
54+
const joke1 =
55+
"What's the difference between Java and JavaScript? Java is like JavaScript the way car is like carpet.";
56+
const punchline1 = "Java is like JavaScript the way car is like carpet.";
57+
assert.deepStrictEqual(getPunchline(joke1), punchline1);
58+
59+
const joke2 =
60+
"Why do Javascript developers prefer dark mode? Because light attracts bugs.";
61+
const punchline2 = "Because light attracts bugs.";
62+
assert.deepStrictEqual(getPunchline(joke2), punchline2);
63+
64+
// Exercise 4 ///////////////////////////////////////////////////////////////////////
65+
// Capitalize first letter of every word in a string, and lowercase the rest
66+
const toTitleCase = (inputString) => {
67+
// Note: you can add groups here to make your job easier later ;-)
68+
const regex = /\b\w\S*\b/g;
69+
70+
// write function here
71+
};
72+
73+
const title1 = "of mice and MEN";
74+
const outputTitle1 = "Of Mice And Men";
75+
assert.deepEqual(toTitleCase(title1), outputTitle1);
76+
77+
const title2 =
78+
"knitting with dog hair: better a sweater from a dog you KNOW and LOVE than from a sheep you’ll NEVER meet";
79+
const outputTitle2 =
80+
"Knitting With Dog Hair: Better A Sweater From A Dog You Know And Love Than From A Sheep You’ll Never Meet";
81+
assert.deepEqual(toTitleCase(title2), outputTitle2);

exercises/5-regex-obj.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
const assert = require("assert");
2+
3+
// Exercise 1 ///////////////////////////////////////////////////////////////////////
4+
// Write a function that creates a regular expression that checks for
5+
// every instance of the input string surrounded by word boundaries (\b)
6+
const myFavoriteStringRegex = (favoriteString) => {
7+
// write the function here
8+
};
9+
10+
// assert has trouble comparing regexes; hence the "toString()"
11+
assert.strictEqual(
12+
myFavoriteStringRegex("regex").toString(),
13+
/\bregex\b/g.toString()
14+
);
15+
16+
// Exercise 2 ///////////////////////////////////////////////////////////////////////
17+
// Return the input regular expression with the 'm' flag removed.
18+
// If there is no 'm' flag, just return the regular expression unaltered.
19+
const removeMultilineFlag = (inputRegex) => {
20+
// write the function here
21+
};
22+
23+
// assert has trouble comparing regexes; hence the "toString()"
24+
assert.strictEqual(removeMultilineFlag(/^a+/m).toString(), /^a+/.toString());
25+
assert.strictEqual(removeMultilineFlag(/^a+/).toString(), /^a+/.toString());

exercises/6-free-for-all.js

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
const assert = require("assert");
2+
3+
// Exercise 1 ///////////////////////////////////////////////////////////////////////
4+
// Return all sentences of the string. Ignore space between sentences. Assume a sentence
5+
// ends with . ? or !
6+
const getSentences = (inputString) => {
7+
// I'm not writing the regex for you here because there are a couple possibilities
8+
// for how to do this and I don't want to sway you one way or the other! Discuss with
9+
// your breakout room. :-)
10+
};
11+
const input1 =
12+
"Some people, when confronted with a problem, think, I know, I'll use regular expressions. Now they have two problems.";
13+
const input1sentence1 =
14+
"Some people, when confronted with a problem, think, I know, I'll use regular expressions.";
15+
const input1sentence2 = "Now they have two problems.";
16+
assert.deepStrictEqual(getSentences(input1), [
17+
input1sentence1,
18+
input1sentence2,
19+
]);
20+
21+
const input2 = "I did it! I found the bug! Now to find the next 99.";
22+
const input2sentences = [
23+
"I did it!",
24+
"I found the bug",
25+
"Now to find the next 99.",
26+
];
27+
assert.deepStrictEqual(getSentences(input2), input2sentences);
28+
29+
// Exercise 2 ///////////////////////////////////////////////////////////////////////
30+
// Given an array of files in a directory, identify which files have a vi swap file (.filename.swp -- for example: .favorite_regexes.txt.swp)
31+
const findFilesWithViSwap = (fileArray) => {
32+
// you may find it useful to add a group to the following regex
33+
const regex = /^\.[\w\.]+\.swp$/;
34+
35+
// write function here
36+
};
37+
38+
const directoryFiles = [
39+
"resume.txt",
40+
".resume.txt.swp",
41+
"cover_letter.txt",
42+
".cover_letter.txt.swp",
43+
"signed_offer_letter.txt",
44+
"robot_to_reply_to_job_postings.js",
45+
".robot_to_reply_to_job_postings.js.swp",
46+
];
47+
assert.deepStrictEqual(findFilesWithViSwap[directoryFiles], [
48+
"resume.txt",
49+
"cover_letter.txt",
50+
"robot_to_reply_to_job_postings.js",
51+
]);
52+
53+
// Exercise 3 ///////////////////////////////////////////////////////////////////////
54+
// Strip html tags out of text
55+
const stripHtmlTags = (inputString) => {
56+
const regex = /<[^<>]*>/gm;
57+
58+
// write function here
59+
};
60+
61+
const html1 = "<span>This is a span</span>";
62+
const stripped1 = "This is a span";
63+
assert.strictEqual(stripHtmlTags(html1), stripped1);
64+
65+
const html2 =
66+
"<h1>Regular Expressions</h1>\n<h2>Quantifiers</h2>\n<p>Quantifiers tell you how many of the preceding token are allowed.</p>";
67+
const stripped2 =
68+
"Regular Expressions\nQuantifiers\nQuantifiers tell you how many of the preceding token are allowed.";
69+
assert.strictEqual(stripHtmlTags(html2), stripped2);
70+
71+
const html3 = "No tags. Absolutely none.";
72+
assert.strictEqual(stripHtmlTags(html3), html3);
73+
74+
// Exercise 4 ///////////////////////////////////////////////////////////////////////
75+
// Given an input array, return an array only with strings that contain no numbers
76+
const filterOutNumberUsernames = (inputArray) => {
77+
const regex = /^\D+$/;
78+
79+
// write function here
80+
};
81+
82+
const usernameArray = [
83+
"githubFan98",
84+
"loves2code",
85+
"dadJokesOnly",
86+
"javascript4ever",
87+
"son!cTheHedgehog",
88+
];
89+
const filteredUsernameArray = ["dadJokesOnly", "son!cTheHedgehog"];
90+
assert.deepStrictEqual(
91+
filterOutNumberUsernames(usernameArray),
92+
filteredUsernameArray
93+
);
94+
95+
// Exercise 5 ///////////////////////////////////////////////////////////////////////
96+
// Make sure the first word of every sentence is capitalized
97+
// For simplicity, you can assume the first word of the string is already capitalized
98+
const capitalizeFirstWordOfSentence = (inputString) => {
99+
// you'll probably want to add groups here
100+
const regex = /[\.\?\!]\s+\w/gs;
101+
102+
// write function here
103+
};
104+
105+
const sentences1 =
106+
"We are just an advanced breed of monkeys on a minor planet of a very average star. but we can understand the Universe. that makes us something very special. ― Stephen Hawking";
107+
const capitalized1 =
108+
"We are just an advanced breed of monkeys on a minor planet of a very average star. But we can understand the Universe. That makes us something very special. ― Stephen Hawking";
109+
assert.strictEqual(capitalizeFirstWordOfSentence(sentences1), capitalized1);
110+
111+
const sentences2 =
112+
"Why was the JavaScript developer sad? because they didn't Node how to Express themself";
113+
const capitalized2 =
114+
"Why was the JavaScript developer sad? Because they didn't Node how to Express themself";
115+
assert.strictEqual(capitalizeFirstWordOfSentence(sentences2), capitalized2);

0 commit comments

Comments
 (0)