Regular expressions Flashcards
What are regular expressions?
Regular expressions are used in programming languages to match parts of strings. You create patterns to help you do that matching.
If you want to find the word the in the string The dog chased the cat, you could use the following regular expression: /the/. Notice that quote marks are not required within the regular expression.
What is the .test() method of testing a regex?
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.
What’s a literal match?
Visualise code in which the regex petRegex looks to match the pets dog, cat, bird, or fish into a string petString.
In regex, what can be used to ignore letter case?
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.
How is a matched regex expression extracted?
By using the .match() method.
In regex, how do you serch/extract a pattern more than once?
Using the regex starRegex, find and extract both Twinkle words from the string twinkleStar.
In regex what is the wildecard character?
In regex how do you match everything?
/./
In regex, how do you match a single character with multiple possibilities?
In regex, how can you match a range of characters?
In regex, how can you match a range of characters and a range of numbers at the same time?
In regex, what is a negated character set?
A set of characters that you do not want to match.
How do you create a negated character set?
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.
In regex, how is a patern or one or more letters found?
What will be the output of the code below?
What does the * do?
In order, the three match calls would return the values [“goooooooo”], [“g”], and null.
It matches zero or more of a character.
In regex, what is greedy matching?
A match that finds the longest possible part of a string that fits the regex pattern and returns it as a match.
Example:
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.
In regex, how do you chage greedy matching to lazy matching?
You can use the ? character to change it to lazy matching. “titanic” matched against the adjusted regex of /t[a-z]*?i/ returns [“ti”].
What should be noted about paring HTML with regex?
Parsing HTML with regular expressions should be avoided, but pattern matching an HTML string with regular expressions is completely fine.