JavaScript Algorithms & Data Structures Flashcards
What is lazy matching in the context of regex?
Lazy means match shortest possible string.
For example, the greedy h.+l matches ‘hell’ in ‘hello’ but the lazy h.+?l matches ‘hel’.
What is greedy matching in the context of regex?
‘Greedy’ means match longest possible string.
For example, the greedy h.+l matches ‘hell’ in ‘hello’ but the lazy h.+?l matches ‘hel’.
What does the code calRegex do below?
let rickyAndCal = “Cal and Ricky both like racing.”;
let calRegex = /^Cal/;
let result = calRegex.test(rickyAndCal);
It uses the caret to search for patterns at the beginning of strings.
What character do you use in regex to search for patterns at the end of strings?
You can search the end of strings using the dollar sign character $ at the end of the regex.
What does \w represent in regex?
It’s a character class and a shortcut that matches uppercase and lowercase letters plus numbers. It also includes the underscore character _.
How do you match the opposite pattern for the shortcut \w in regex?
“\W”
This shortcut is the same as [^A-Za-z0-9_] which matches everything that’s the opposite of /w.
What is the regex shortcut for just digits and numbers?
“\d”
What is the regex shortcut when searching for non-digits?
“\D|
How do you search for whitespace using regex?
“\s”
What are quantity specifiers in regex for?
Quantity specifiers aka “{ }” are used for matching a certain range of patterns.
What is the question mark used for in expressions?
The question mark checks for zero or one of the preceding element. It’s another way of saying the preceding element is optional.
What are capture groups used for and how do we use them?
Capture groups are used to find repeated substrings. They can bed used by enclosing parentheses.
Example: /(\w+)/
What does the replace method do?
In the context of regex, it’s used to replace the text you match.
What does the test method do?
It tests a regex and returns true or false depending on if the regex is found in a string.
What does the g flag indicate?
The g flag indicates that the regular expression should be tested against all possible matches in a string.