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.
![](https://s3.amazonaws.com/brainscape-prod/system/cm/387/952/441/a_image_thumb.png?1655475275)
What’s a literal match?
![](https://s3.amazonaws.com/brainscape-prod/system/cm/387/952/753/a_image_thumb.png?1655498753)
Visualise code in which the regex petRegex looks to match the pets dog, cat, bird, or fish into a string petString.
![](https://s3.amazonaws.com/brainscape-prod/system/cm/387/983/591/a_image_thumb.png?1655499211)
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.
![](https://s3.amazonaws.com/brainscape-prod/system/cm/387/985/042/a_image_thumb.png?1655500129)
In regex, how do you serch/extract a pattern more than once?
![](https://s3.amazonaws.com/brainscape-prod/system/cm/387/985/129/a_image_thumb.png?1655500376)
Using the regex starRegex, find and extract both Twinkle words from the string twinkleStar.
![](https://s3.amazonaws.com/brainscape-prod/system/cm/387/985/452/q_image_thumb.png?1655500576)
![](https://s3.amazonaws.com/brainscape-prod/system/cm/387/985/452/a_image_thumb.png?1655500586)
In regex what is the wildecard character?
![](https://s3.amazonaws.com/brainscape-prod/system/cm/387/986/018/a_image_thumb.png?1655500929)
In regex how do you match everything?
/./
In regex, how do you match a single character with multiple possibilities?
![](https://s3.amazonaws.com/brainscape-prod/system/cm/387/986/823/a_image_thumb.png?1655501318)
In regex, how can you match a range of characters?
![](https://s3.amazonaws.com/brainscape-prod/system/cm/387/986/941/a_image_thumb.png?1655501992)
In regex, how can you match a range of characters and a range of numbers at the same time?
![](https://s3.amazonaws.com/brainscape-prod/system/cm/388/077/262/a_image_thumb.png?1655589338)
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?
![](https://s3.amazonaws.com/brainscape-prod/system/cm/388/172/788/a_image_thumb.png?1655671050)
What will be the output of the code below?
What does the * do?
![](https://s3.amazonaws.com/brainscape-prod/system/cm/388/176/065/q_image_thumb.png?1655674106)
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.