It’s Friday! And I have to start dinner here soon. We are having Mediterranean Chicken with pasta. That’s all I’m thinking about is making dinner so I can’t really think of other things to say. I think that is my ADHD. My therapist took off anxiety from my diagnoses. He thinks my anxiety can be from ADHD and PTSD. So he took of general anxiety disorder unless there is a change that he sees to put it back in. I have a Monday appointment at 10am. I have such a hard time talking during therapy. I’m glad he asks a lot of questions cause it’s hard for me to just talk about what is on my mind. The past is hard to talk about too. But I promise I will talk about things. It’s hard for me to come to terms that, yes, I was abused. My mind doesn’t like to think about that. I want to feel stronger than that. It makes me feel ashamed and angry that I even allowed things to happen. I’m already small. I don’t want to feel small as well.
I feel like my medication is working. I know, should be about three weeks until an effect is made. But I don’t think my medication was completely out of my system to begin with. I do feel better. Not as irritated and angry as I was without the medication.
JavaScript today was pretty easy. But I just wish that we had more than one page of instructions and more practice for the different subjects we are doing. I want more practice.
Ok, Tommy is off of work. I better get dinner started.
—————————-
Using the hyphen (-) to match a range of characters is not limited to letters. It also works to match a range of numbers.
For example, /[0-5]/ matches any number between 0 and 5, including the 0 and 5.
Also, it is possible to combine a range of letters and numbers in a single character set.
let jennyStr = "Jenny8675309"; let myRegex = /[a-z0-9]/ig; jennyStr.match(myRegex);
So far, you have created a set of characters that you want to match, but you could also create a set of characters that you do not want to match. These types of character sets are called negated character sets.
To create a negated character set, you place a caret character (^) after the opening bracket and before the characters you do not want to match.
For example, /[^aeiou]/gi matches all characters that are not a vowel. Note that characters like ., !, [, @, / and white space are matched – the negated vowel character set only excludes the vowel characters.
Sometimes, you need to match a character (or group of characters) that appears one or more times in a row. This means it occurs at least once, and may be repeated.
You can use the + character to check if that is the case. Remember, the character or pattern has to be present consecutively. That is, the character has to repeat one after the other.
For example, /a+/g would find one match in abc and return [“a”]. Because of the +, it would also find a single match in aabc and return [“aa”].
If it were instead checking the string abab, it would find two matches and return [“a”, “a”] because the a characters are not in a row – there is a b between them. Finally, since there is no a in the string bcd, it wouldn’t find a match.
The last challenge used the plus + sign to look for characters that occur one or more times. There’s also an option that matches characters that occur zero or more times.
The character to do this is the asterisk or star: *.
let soccerWord = "gooooooooal!"; let gPhrase = "gut feeling"; let oPhrase = "over the moon"; let goRegex = /go*/; soccerWord.match(goRegex); gPhrase.match(goRegex); oPhrase.match(goRegex);
In order, the three match calls would return the values [“goooooooo”], [“g”], and null.
In regular expressions, a greedy match finds the longest possible part of a string that fits the regex pattern and returns it as a match. The alternative is called a lazy match, which finds the smallest possible part of the string that satisfies the regex pattern.
You can apply the regex /t[a-z]*i/ to the string “titanic”. This regex is basically a pattern that starts with t, ends with i, and has some letters in between.
Regular expressions are by default greedy, so the match would return [“titani”]. It finds the largest sub-string possible to fit the pattern.
However, you can use the ? character to change it to lazy matching. “titanic” matched against the adjusted regex of /t[a-z]*?i/ returns [“ti”].
Note: Parsing HTML with regular expressions should be avoided, but pattern matching an HTML string with regular expressions is completely fine.
Time to pause and test your new regex writing skills. A group of criminals escaped from jail and ran away, but you don’t know how many. However, you do know that they stay close together when they are around other people. You are responsible for finding all of the criminals at once.
Here’s an example to review how to do this:
The regex /z+/ matches the letter z when it appears one or more times in a row. It would find matches in all of the following strings:
"z" "zzzzzz" "ABCzzzz" "zzzzABC" "abczzzzzzzzzzzzzzzzzzzzzabc"
But it does not find matches in the following strings since there are no letter z characters:
"" "ABC" "abcabc"
Prior challenges showed that regular expressions can be used to look for a number of matches. They are also used to search for patterns in specific positions in strings.
In an earlier challenge, you used the caret character (^) inside a character set to create a negated character set in the form [^thingsThatWillNotBeMatched]. Outside of a character set, the caret is used to search for patterns at the beginning of strings.
let firstString = "Ricky is first and can be found."; let firstRegex = /^Ricky/; firstRegex.test(firstString); let notFirst = "You can't find Ricky now."; firstRegex.test(notFirst);
The first test call would return true, while the second would return false.