I’m currently at Lexi’s school, having surprised her today. We drove down yesterday. We need her hearing aid paired with her other at the doctor’s office. Being Bluetooth hearing aids, they need to be paired before they can work again. Then, we will mail her hearing aids back to her. I hate leaving her without hearing aids, but we must get this done so she will have them. We will see her later today.
Tommy and I are studying in the school library. He is studying Red Hat (a Linux distro), and I’m tackling the challenge of regular expressions in coding. It’s proving to be quite the obstacle, as it involves creating a sequence of characters to match a pattern in text. Even with extensive studying, memorizing every line of code is impossible, so I consistently rely on Google to assist me. I am finishing a spam filter project and moving on to a shopping cart code project to learn more about object-oriented programming.
The wind is picking up today! Whenever I step outside, I feel like it’s pushing me around. It’s funny how quickly the weather can change – it was well over 80 degrees just yesterday, and now it’s only 60. That’s what spring is all about, though – unpredictable weather.
I’m looking forward to warmer temperatures. I enjoy the contrast between cold weather outside and a warm, cozy indoor environment. Still, I’m definitely ready for some sunshine. Of course, there’s nothing stopping me from enjoying a hot cup of tea or coffee even when it’s warm outside – I might just switch to iced tea or coffee instead! Maybe. I do like hot tea better than iced tea.
JavaScript notes…
————————————
FreeCodeCamp Spam Filter steps 16 – 35
Step 16
The dollar value may be more than one digit. To match this, the + quantifier can be used – this matches one or more consecutive occurrence. For example, the regular expression /a+/ matches one or more consecutive a characters.
Update your regular expression to match one or more consecutive digits.
const dollarRegex = /[0-9]+ dollars/i;
Step 17
Between your digits and your dollars text, you want to catch place values.
Use the | token to allow hundred, thousand, million, or billion between your digits and dollars.
const dollarRegex = /[0-9]+ hundred|thousand|million|billion dollars/i;
Step 18
A capture group is a way to define a part of the expression that should be captured and saved for later reference. You can define a capture group by wrapping a part of your expression in parentheses. For example, /h(i|ey) camper/ would match either hi camper or hey camper, and would capture i or ey in a group.
Turn your place values into a capture group.
const dollarRegex = /[0-9]+ (hundred|thousand|million|billion) dollars/i;
Step 19
Now that you have your capture group, you can mark the entire pattern as an optional match. The ? quantifier matches zero or one occurrence of the preceding character or group. For example, the regular expression /colou?r/ matches both color and colour, because the u is optional.
Mark your capture group as optional.
const dollarRegex = /[0-9]+ (hundred|thousand|million|billion)? dollars/i;
Step 20
One last thing with this expression. You don’t actually need the match value from your capture group, so you can turn it into a non-capturing group. This will allow you to group the characters together without preserving the result.
To create a non-capturing group in a regular expression, you can add ?: after the opening parenthesis of a group. For instance, (?:a|b) will match either a or b, but it will not capture the result.
Update your regular expression to use a non-capturing group.
const dollarRegex = /[0-9]+ (?:hundred|thousand|million|billion)? dollars/i;
Step 21
Your next regular expression will look for strings like free money. Start by declaring a freeRegex variable and assigning it a regular expression that will match the string free money. Remember to make it case-insensitive.
const freeRegex = /free money/i;
Step 22
Add your new regular expression to your denyList array so you can test it.
const denyList = [helpRegex, dollarRegex, freeRegex];
Step 23
Spam messages often use numbers instead of letters to bypass filters. Your regular expression should catch these.
Replace the e characters in your regular expression with character classes that match e and 3.
const freeRegex = /fr[e3][e3] mon[e3]y/i;
Step 24
Now update your o character to match o and 0 (the digit).
const freeRegex = /fr[e3][e3] m[o0]n[e3]y/i;
Step 25
Your regex should match whole words, not partial words. That is, you do not want to match hands-free money management.
To do this, start by checking for spaces before and after your pattern. You can do this by using the meta character \s, which will match spaces, tabs, and line breaks.
const freeRegex = /\sfr[e3][e3] m[o0]n[e3]y\s/i;
Step 26
If you try entering the message free money, you’ll notice it doesn’t match your expression! This is because \s doesn’t match the beginning or end of the text.
To match the beginning of the text, you can use the ^ anchor. This asserts that your pattern match starts at the beginning of the full string.
Replace your first \s character with a non-capturing group that matches \s or ^.
const freeRegex = /(?:\s|^)fr[e3][e3] m[o0]n[e3]y\s/i;
Step 27
You still aren’t matching free money yet, because you need to match the end of the string as well.
Like the ^ anchor, you can use the $ anchor to match the end of the string.
Update your regular expression to match either the end of the string or a space, like you did for the beginning of the string.
const freeRegex = /(?:^|\s)fr[e3][e3] m[o0]n[e3]y(?:\s|$)/i;
Step 28
Your next regular expression will match strings like stock alert. Declare a stockRegex variable and assign it a regular expression that will match the string stock alert. Remember to make it case insensitive.
Add it to your denyList array as well.
const stockRegex = /stock alert/i; const denyList = [helpRegex, dollarRegex, freeRegex, stockRegex];
Step 29
Like your freeRegex, update your stockRegex to replace the e and o characters with character classes to match the letter and the corresponding number.
const stockRegex = /st[o0]ck al[e3]rt/i;
Step 30
Next update your s and t characters to also match 5 and 7 respectively.
const stockRegex = /[s5][t7][o0]ck al[e3]r[t7]/i;
Step 31
Character classes can take more than two characters. Replace your a character with a character class that matches a, @, and 4.
const stockRegex = /[s5][t7][o0]ck [a@4]l[e3]r[t7]/i;
Step 32
Using the same syntax, update c to match c, {, [, and (.
const stockRegex = /[s5][t7][o0][c{[(]k [a@4]l[e3]r[t7]/i;
Step 33
Finally, allow your regex to match whole words (like you did with freeRegex).
const stockRegex = /(?:^|\s)[s5][t7][o0][c{[(]k [a@4]l[e3]r[t7](?:$|\s)/i;
tep 34
Your final regular expression will look for strings like dear friend. Declare a dearRegex and assign it a regular expression that will match the string dear friend. Remember to make it case insensitive, and add it to your denyList array.
const dearRegex = /dear friend/i; const denyList = [helpRegex, dollarRegex, freeRegex, stockRegex, dearRegex];
Step 35
To put everything you have learned together, update your dearRegex to map the vowels to the corresponding numbers (note that i should match 1, and also match the pipe symbol |), and to match whole words.
With that, your spam filter project is complete.
const dearRegex = /(?:^|\s)d[e3][a@4]r fr[i1|][e3]nd(?:$|\s)/i;