It’s been a little while since I posted. Tommy had his neck surgery a few Tuesdays ago. He is feeling a lot better. He stayed a few extra days in the hospital as they waited for his blood oxygen to rise. He had a chest x-ray and they noted some fluid around his heart. One doctor said it was congestive heart failure, then the cardiologist came in and figured that from Tom’s visit to the hospital in Denver and his four hour surgery that caused the fluid retention and it isn’t congestive heart failure. Made me angry that the doctor scared him and us without so much as a second opinion. I know he is going to be ok now. So he is on lasic now to get rid of that fluid. He has a cardiologist appointment tomorrow. I just hope they put his mind to ease. He has been feeling a lot better these last two weeks and his feet and legs have gone down from their puffiness. He looks a lot better now.
We have been taking a walk everyday to get in some exercise. We started slow and came to two miles now. The walk down the path next to our house is pretty easy cause it is mostly downhill. The walk back uphill towards home is the killer part. But it feels good to get some exercise in.
Merlin is getting much bigger. We are waiting for him to take his last round of shots before we take him to training class. He still has to put everything into his mouth.
The kids start back up with school soon. Alex already started his senior year of high school.
Another crash happened on our street. Just now. The driver spun out of control and took down some of the fence. This isn’t the first time our fence was taken down by a crash on our street. We gave the police the landlord’s name. People need to slow down. The speed limit is only 35mph, these crashes shouldn’t be happening. We are ok though.
JavaScript is a bit hard today. The material and getting back into the groove of studying.
JavaScript notes…
——————————————-
Sometimes the patterns you want to search for may have parts of it that may or may not exist. However, it may be important to check for them nonetheless.
You can specify the possible existence of an element with a question mark, ?. This checks for zero or one of the preceding element. You can think of this symbol as saying the previous element is optional.
For example, there are slight differences in American and British English and you can use the question mark to match both spellings.
let american = "color"; let british = "colour"; let rainbowRegex= /colou?r/; rainbowRegex.test(american); rainbowRegex.test(british);
Both uses of the test method would return true.
Lookaheads are patterns that tell JavaScript to look-ahead in your string to check for patterns further along. This can be useful when you want to search for multiple patterns over the same string.
There are two kinds of lookaheads: positive lookahead and negative lookahead.
A positive lookahead will look to make sure the element in the search pattern is there, but won’t actually match it. A positive lookahead is used as (?=…) where the … is the required part that is not matched.
On the other hand, a negative lookahead will look to make sure the element in the search pattern is not there. A negative lookahead is used as (?!…) where the … is the pattern that you do not want to be there. The rest of the pattern is returned if the negative lookahead part is not present.
Lookaheads are a bit confusing but some examples will help.
let quit = "qu"; let noquit = "qt"; let quRegex= /q(?=u)/; let qRegex = /q(?!u)/; quit.match(quRegex); noquit.match(qRegex);
Both of these match calls would return [“q”].
A more practical use of lookaheads is to check two or more patterns in one string. Here is a (naively) simple password checker that looks for between 3 and 6 characters and at least one number:
let password = "abc123"; let checkPass = /(?=\w{3,6})(?=\D*\d)/; checkPass.test(password);
Use lookaheads in the pwRegex to match passwords that are greater than 5 characters long, and have two consecutive digits.
Sometimes we want to check for groups of characters using a Regular Expression and to achieve that we use parentheses ().
If you want to find either Penguin or Pumpkin in a string, you can use the following Regular Expression: /P(engu|umpk)in/g
Then check whether the desired string groups are in the test string by using the test() method.
let testStr = "Pumpkin"; let testRegex = /P(engu|umpk)in/; testRegex.test(testStr);
The test method here would return true.
Say you want to match a word that occurs multiple times like below.
let repeatStr = "row row row your boat";
You could use /row row row/, but what if you don’t know the specific word repeated? Capture groups can be used to find repeated substrings.
Capture groups are constructed by enclosing the regex pattern to be captured in parentheses. In this case, the goal is to capture a word consisting of alphanumeric characters so the capture group will be \w+ enclosed by parentheses: /(\w+)/.
The substring matched by the group is saved to a temporary “variable”, which can be accessed within the same regex using a backslash and the number of the capture group (e.g. \1). Capture groups are automatically numbered by the position of their opening parentheses (left to right), starting at 1. The example below matches a word that occurs thrice separated by spaces:
let repeatRegex = /(\w+) \1 \1/; repeatRegex.test(repeatStr); // Returns true repeatStr.match(repeatRegex); // Returns ["row row row", "row"]
Using the .match() method on a string will return an array with the matched substring, along with its captured groups.