RegExp Flashcards

1
Q

Regular expressions are delimited by two ____ .

A

forward slash characters.
Ex: / /

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

String.prototype.match()

A

Matching an instance of a string against a regular expression and returns an array of matched values or null if no matches are found.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is alternation? What is the (most basic) syntax?

A

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)/

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is a character class? What is the syntax?

A

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 [ ].

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is a negated character class?

A

A negated character class’s first character is a ^ and matches all characters NOT identified within the character class (between [ ])

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

\n

A

represents a line feed, instructing the device to move the cursor down to the next line.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

.

A

Is a meta-character that matches any character (includes whitespace, punctuation, etc..).

Note! when used inside [ ] it is interpreted as a literal period character.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

\s

A

Matches any whitespace character which includes tabs, new lines, etc.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

\S

A

Matches any NON-whitespace character.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

\d

A

Matches any decimal digit (0-9)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

\D

A

Matches any NON-decimal digit character.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

\w and \W

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is an anchor?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What anchor symbols are used to match the beginning or ending of a string?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

m flag

A

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 (^, $).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

A word boundary occurs…

A

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.

17
Q

\b

A

Anchor that matches to word boundaries.

18
Q

*

A

Quantifier used to match 0 or more occurrences of the pattern to its left.

19
Q

+

A

Quantifier used to match 1 or more occurrences of the pattern to its left.

20
Q

?

A

Quantifier used to match 0 or 1 occurrences of the pattern to its left.

21
Q

What is a quantifier?

A

A quantifier is a meta-character that specifies the number of occurrences of a pattern.

22
Q

What is a range quantifier? What is its syntax?

A

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.

23
Q

A quantifier is described as greedy when…

A

They always match the longest possible string they can.

24
Q

String.prototype.split()

A

Takes a pattern, which may be a RegExp, and divides the string into an array of substrings which is then returned.

25
Q

String.prototype.replace()

A

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’.

26
Q

In the ____ mode (by default) a quantified character is repeated as many times as ____.

A

greedy, possible

27
Q

RegExp.prototype.test()

A

takes a string as an argument and returns true if there is at least one pattern match and false otherwise.

28
Q

Explain why the following result occurs:
"Hello, Java!".match(/\bJava!\b/) ); // null (no match)

A

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.

29
Q
A