RegExp Flashcards
Regular expressions are delimited by two ____ .
forward slash characters.
Ex: / /
String.prototype.match()
Matching an instance of a string against a regular expression and returns an array of matched values or null if no matches are found.
What is alternation? What is the (most basic) syntax?
Alternation is a simple way to specify multiple alternatives for a pattern match.
The most basic syntax is two or more patterns separated by the |
character and then the entire expression is surrounded by parentheses.
EX: /(cat|dog|rabbit)/
What is a character class? What is the syntax?
A character class is a way to specify a set of characters that you want to match in a pattern. It allows you to define a range of characters or a list of characters that the regex engine should match against.
Character classes are denoted by enclosing the characters within [ ]
.
What is a negated character class?
A negated character class’s first character is a ^
and matches all characters NOT identified within the character class (between [ ]
)
\n
represents a line feed, instructing the device to move the cursor down to the next line.
.
Is a meta-character that matches any character (includes whitespace, punctuation, etc..).
Note! when used inside [ ]
it is interpreted as a literal period character.
\s
Matches any whitespace character which includes tabs, new lines, etc.
\S
Matches any NON-whitespace character.
\d
Matches any decimal digit (0-9)
\D
Matches any NON-decimal digit character.
\w
and \W
Matches ‘word characters’ which includes all alphabetic characters (a-z, A-Z), all decimal digits (0-9), and an underscore (_
).
\W
matches any NON-word character.
What is an anchor?
Anchors provide a way to limit how a regex matches a particular string by telling the regex engine where matches can begin and where they can end.
What anchor symbols are used to match the beginning or ending of a string?
The ^
matches the beginning of a string.
AKA: The entire regex must match starting at the beginning of the string.
The $
matches the ending of a string.
m
flag
Is a flag which has the engine treat embedded newline characters as separate lines. This has specific use case when pattern matching to the beginning or end of a string with anchors (^, $).