JavaScript Algorithms & Data Structures Flashcards

1
Q

What is lazy matching in the context of regex?

A

Lazy means match shortest possible string.

For example, the greedy h.+l matches ‘hell’ in ‘hello’ but the lazy h.+?l matches ‘hel’.

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

What is greedy matching in the context of regex?

A

‘Greedy’ means match longest possible string.

For example, the greedy h.+l matches ‘hell’ in ‘hello’ but the lazy h.+?l matches ‘hel’.

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

What does the code calRegex do below?

let rickyAndCal = “Cal and Ricky both like racing.”;
let calRegex = /^Cal/;
let result = calRegex.test(rickyAndCal);

A

It uses the caret to search for patterns at the beginning of strings.

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

What character do you use in regex to search for patterns at the end of strings?

A

You can search the end of strings using the dollar sign character $ at the end of the regex.

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

What does \w represent in regex?

A

It’s a character class and a shortcut that matches uppercase and lowercase letters plus numbers. It also includes the underscore character _.

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

How do you match the opposite pattern for the shortcut \w in regex?

A

“\W”

This shortcut is the same as [^A-Za-z0-9_] which matches everything that’s the opposite of /w.

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

What is the regex shortcut for just digits and numbers?

A

“\d”

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

What is the regex shortcut when searching for non-digits?

A

“\D|

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

How do you search for whitespace using regex?

A

“\s”

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

What are quantity specifiers in regex for?

A

Quantity specifiers aka “{ }” are used for matching a certain range of patterns.

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

What is the question mark used for in expressions?

A

The question mark checks for zero or one of the preceding element. It’s another way of saying the preceding element is optional.

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

What are capture groups used for and how do we use them?

A

Capture groups are used to find repeated substrings. They can bed used by enclosing parentheses.

Example: /(\w+)/

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

What does the replace method do?

A

In the context of regex, it’s used to replace the text you match.

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

What does the test method do?

A

It tests a regex and returns true or false depending on if the regex is found in a string.

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

What does the g flag indicate?

A

The g flag indicates that the regular expression should be tested against all possible matches in a string.

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

What does the g flag indicate?

A

The g flag indicates that the regular expression should be tested against all possible matches in a string.