I like doing regular expressions in JavaScript. It’s easier to understand.
I have my medication back. Of course it’s going to take about three weeks of taking it for the meds to take effect. But I do feel better having my medication back. I can get my moods taken care of and can focus better. I have a new medication for nightmares that I take at night.
Merlin is being good today. He’s been with Karissa most of the day. She has both Merlin and Emily, Chris’s cat, in her room. Apparently Emily is ok with Merlin being in the same room as long as he leaves her alone. Sandy is the same way. She isn’t quite sure about Merlin but as long as he leaves her alone, she is ok. I definitely watch Merlin around Sandy. Sandy won’t put up with him.
This Sunday is tea time. And it will definitely be Winnie the Pooh themed. I’m curious to how they will decorate the place.
We are having chicken lettuce wraps for dinner. I will probably start around 5:30 to cut up the chicken. I’m so slow at prepping food. But I have to have it prepped before starting to cook. They make the cooking and prepping look really easy and fast on cooking shows. You know they just edit out all the extra time that prepping the food took. I was looking at my Tasting History cookbook. Some of the ingredients in these recipes aren’t readily available at your local grocery store. But I guess there is always Amazon? So some of these recipes would need to be thought out in advance if I need to go through Amazon for some of the ingredients.
I’ve been watching Tommy play Final Fantasy XVI, and I’m liking the story so far. I keep wanting to see the walkthrough but I don’t want to spoil myself.
Tommy, Kelly, Chris and I are heading up to Phoenix in a few weekends to see the movie Oppenheimer in the IMAX theater. The last time I was in an IMAX theater was when I was pregnant with Karissa. Kevin and I went to Texas to see Disney’s Fantasia.
I will be back later…
—————————————–
Up until now, you’ve looked at regexes to do literal matches of strings. But sometimes, you might want to also match case differences.
Case (or sometimes letter case) is the difference between uppercase letters and lowercase letters. Examples of uppercase are A, B, and C. Examples of lowercase are a, b, and c.
You can match both cases using what is called a flag. There are other flags but here you’ll focus on the flag that ignores case – the i flag. You can use it by appending it to the regex. An example of using this flag is /ignorecase/i. This regex can match the strings ignorecase, igNoreCase, and IgnoreCase.
So far, you have only been checking if a pattern exists or not within a string. You can also extract the actual matches you found with the .match() method.
To use the .match() method, apply the method on a string and pass in the regex inside the parentheses.
Here’s an example:
"Hello, World!".match(/Hello/); let ourStr = "Regular expressions"; let ourRegex = /expressions/; ourStr.match(ourRegex);
Here the first match would return [“Hello”] and the second would return [“expressions”].
Note that the .match syntax is the “opposite” of the .test method you have been using thus far:
'string'.match(/regex/); /regex/.test('string');
So far, you have only been able to extract or search a pattern once.
let testStr = "Repeat, Repeat, Repeat"; let ourRegex = /Repeat/; testStr.match(ourRegex);
Here match would return [“Repeat”].
To search or extract a pattern more than once, you can use the global search flag: g.
let repeatRegex = /Repeat/g; testStr.match(repeatRegex);
And here match returns the value [“Repeat”, “Repeat”, “Repeat”]
Sometimes you won’t (or don’t need to) know the exact characters in your patterns. Thinking of all words that match, say, a misspelling would take a long time. Luckily, you can save time using the wildcard character: .
The wildcard character . will match any one character. The wildcard is also called dot and period. You can use the wildcard character just like any other character in the regex. For example, if you wanted to match hug, huh, hut, and hum, you can use the regex /hu./ to match all four words.
let humStr = "I'll hum a song"; let hugStr = "Bear hug"; let huRegex = /hu./; huRegex.test(humStr); huRegex.test(hugStr);
Both of these test calls would return true.
You learned how to match literal patterns (/literal/) and wildcard character (/./). Those are the extremes of regular expressions, where one finds exact matches and the other matches everything. There are options that are a balance between the two extremes.
You can search for a literal pattern with some flexibility with character classes. Character classes allow you to define a group of characters you wish to match by placing them inside square ([ and ]) brackets.
For example, you want to match bag, big, and bug but not bog. You can create the regex /b[aiu]g/ to do this. The [aiu] is the character class that will only match the characters a, i, or u.
let bigStr = "big"; let bagStr = "bag"; let bugStr = "bug"; let bogStr = "bog"; let bgRegex = /b[aiu]g/; bigStr.match(bgRegex); bagStr.match(bgRegex); bugStr.match(bgRegex); bogStr.match(bgRegex);
In order, the four match calls would return the values [“big”], [“bag”], [“bug”], and null.
You saw how you can use character sets to specify a group of characters to match, but that’s a lot of typing when you need to match a large range of characters (for example, every letter in the alphabet). Fortunately, there is a built-in feature that makes this short and simple.
Inside a character set, you can define a range of characters to match using a hyphen character: -.
For example, to match lowercase letters a through e you would use [a-e].
let catStr = "cat"; let batStr = "bat"; let matStr = "mat"; let bgRegex = /[a-e]at/; catStr.match(bgRegex); batStr.match(bgRegex); matStr.match(bgRegex);
In order, the three match calls would return the values [“cat”], [“bat”], and null.