Regular Expressions Flashcards
How do you use a regular expression to determine if a string contains text?
One way to test a regex is using the .test() method. The .test() method takes the regex, applies it to a string (which is placed inside the parentheses), and returns true or false if your pattern finds something or not.
let testStr = “freeCodeCamp”;
let testRegex = /Code/;
testRegex.test(testStr);
Are regular expressions case sensitive?
Yes
What’s the syntax for searching for a string for multiple patterns?
This is powerful to search single strings, but it’s limited to only one pattern. You can search for multiple patterns using the alternation or OR operator: |.
This operator matches patterns either before or after it. For example, if you wanted to match “yes” or “no”, the regex you want is /yes|no/.
How do you ignore case while searching a string?
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.
How do you extract matches from a string?
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/); // Returns ["Hello"]
How do you extract or match multiple patterns in a string?
To search or extract a pattern more than once, you can use the g flag.
let repeatRegex = /Repeat/g;
testStr.match(repeatRegex);
What is the wildcard character and what does it do?
The wildcard character . will match any one character. The wildcard is also called dot and period.
How do search a pattern for multiple possibilities?
ou 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”.
How do you search a range of characters?
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].
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.
How do you create a set of characters that you don’t want to match?
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.
How do you match characters one or more times?
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”].
How do you match characters that occur zero 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); // Returns ["goooooooo"] gPhrase.match(goRegex); // Returns ["g"] oPhrase.match(goRegex); // Returns null
Do you regular expressions by default find the longest or shortest possible match?
Regular expressions are by default greedy, so the match would return [“titani”]. It finds the largest sub-string possible to fit the pattern.
How do you find the shortest possible match?
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”].
How do you search for patterns at the beginning of strings?
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);