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 (^, $).
A word boundary occurs…
1). between any pair of characters, one of which is a word character and one which is not.
2). at the beginning of a string if the first character is a word character.
3). at the end of a string if the last character is a word character.
\b
Anchor that matches to word boundaries.
*
Quantifier used to match 0 or more occurrences of the pattern to its left.
+
Quantifier used to match 1 or more occurrences of the pattern to its left.
?
Quantifier used to match 0 or 1 occurrences of the pattern to its left.
What is a quantifier?
A quantifier is a meta-character that specifies the number of occurrences of a pattern.
What is a range quantifier? What is its syntax?
A range quantifier is used to specify the number of occurrences of a pattern.
A range quantifier consists of a pair of curly braces {}
with one or two numbers and an optional comma, which alters its behavior.
1). p{m} matches precisely m occurrences of the pattern p.
2). p{m,} matches m or more occurrences of p.
3). p{m,n} matches m or more occurrences of p, but not more than n.
A quantifier is described as greedy when…
They always match the longest possible string they can.
String.prototype.split()
Takes a pattern, which may be a RegExp, and divides the string into an array of substrings which is then returned.
String.prototype.replace()
Returns a new string with one, some, or all matches of a pattern replaced with the replacement string.
‘I am a string’.replace(‘I am’, ‘This) will return ‘This a string’.
In the ____ mode (by default) a quantified character is repeated as many times as ____.
greedy, possible
RegExp.prototype.test()
takes a string as an argument and returns true if there is at least one pattern match and false otherwise.
Explain why the following result occurs:"Hello, Java!".match(/\bJava!\b/) ); // null (no match)
The reason why .match returns null is because there is no word boundary between the !
and the end of the string. A word boundary only occurs at the beginning and end of a string if the first/last character is a word character.