Regular expressions Flashcards

1
Q

What are regular expressions?

A

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.

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

What is the .test() method of testing a regex?

A

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.

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

What’s a literal match?

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

Visualise code in which the regex petRegex looks to match the pets dog, cat, bird, or fish into a string petString.

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

In regex, what can be used to ignore letter case?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How is a matched regex expression extracted?

A

By using the .match() method.

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

In regex, how do you serch/extract a pattern more than once?

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

Using the regex starRegex, find and extract both Twinkle words from the string twinkleStar.

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

In regex what is the wildecard character?

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

In regex how do you match everything?

A

/./

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

In regex, how do you match a single character with multiple possibilities?

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

In regex, how can you match a range of characters?

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

In regex, how can you match a range of characters and a range of numbers at the same time?

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

In regex, what is a negated character set?

A

A set of characters that you do not want to match.

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

How do you create a negated character set?

A

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.

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

In regex, how is a patern or one or more letters found?

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

What will be the output of the code below?

What does the * do?

A

In order, the three match calls would return the values [“goooooooo”], [“g”], and null.

It matches zero or more of a character.

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

In regex, what is greedy matching?

A

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.

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

In regex, how do you chage greedy matching to lazy matching?

A

You can use the ? character to change it to lazy matching. “titanic” matched against the adjusted regex of /t[a-z]*?i/ returns [“ti”].

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

What should be noted about paring HTML with regex?

A

Parsing HTML with regular expressions should be avoided, but pattern matching an HTML string with regular expressions is completely fine.

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

Fix the regex // to return the HTML tag

#

A
22
Q

What’s the output of the code below?

A

The first test call would return true, while the second would return false.

23
Q

What’s the output of the code below?

A

The first test call would return true, while the second would return false.

24
Q

In regex, what does \w mean?

A

It is a shorthand for [A-Za-z0-9_].

These shortcut character classes are also known as shorthand character classes.

25
Q

Use the shorthand character class \w to count the number of alphanumeric characters in various quotes and strings.

A
26
Q

What’s the opposite of the shorthand \w?

A

\W (capital w)

27
Q

In regex, what’s the shorthand for finding all numbers?

A

\d(small d)

28
Q

In regex, what’s the shorthand for finding all non-numbers?

A

\D(capital d)

29
Q

You need to check all the usernames in a database. Here are some simple rules that users have to follow when creating their username.

  • Usernames can only use alpha-numeric characters.
  • The only numbers in the username have to be at the end. There can be zero or more of them at the end. Username cannot start with the number.
  • Username letters can be lowercase and uppercase.
  • Usernames have to be at least two characters long. A two-character username can only use alphabet letters as characters.

Change the regex userCheck to fit the constraints listed above.

A
30
Q

In regex, how can whitespace be matched?

A

Using \s, which is a lowercase s. This pattern also matches carriage return, tab, form feed, and new line characters. You can think of it as similar to the character class [\r\t\f\n\v].

31
Q

In regex, what do the .match() and .test() methods return?

A

They return an array and a boolean, respectively.

32
Q

In regex, how are non-whitespace be matched?

A

\S(capital s)

33
Q

In regex, what are quantity specifiers?

A

You can specify the lower and upper number of patterns with quantity specifiers. Quantity specifiers are used with curly brackets ({ and }). You put two numbers between the curly brackets - for the lower and upper number of patterns.

For example, to match only the letter a appearing between 3 and 5 times in the string ah, your regex would be /a{3,5}h/.

34
Q

In regex’ quantity specifiers, how can the lower number of matches alone be specified?

A

To only specify the lower number of patterns, keep the first number followed by a comma.

For example, to match only the string hah with the letter a appearing at least 3 times, your regex would be /ha{3,}h/.

35
Q

In regex’ quantity specifiers, how can the exact number be specified?

A

To specify a certain number of patterns, just have that one number between the curly brackets.

For example, to match only the word hah with the letter a 3 times, your regex would be /ha{3}h/.

36
Q

In regex, how can you specify the possible existence of an element?

A

You can specify the possible existence of an element with a question mark, ?. This checks for zero or one of the preceding element. You can think of this symbol as saying the previous element is optional.

For example, there are slight differences in American and British English and you can use the question mark to match both spellings.

37
Q

In regex, what are lookaheads?

A

Lookaheads are patterns that tell JavaScript to look-ahead in your string to check for patterns further along. This can be useful when you want to search for multiple patterns over the same string.

38
Q

In regex, what types of lookahead are there?

A

Positive lookahead and negative lookahead

39
Q

In regex, what is a positive lookahead?

A

A positive lookahead will look to make sure the element in the search pattern is there, but won’t actually match it. A positive lookahead is used as (?=…) where the … is the required part that is not matched.

40
Q

In regex, what is a negative lookahead?

A

A negative lookahead will look to make sure the element in the search pattern is not there. A negative lookahead is used as (?!…) where the … is the pattern that you do not want to be there. The rest of the pattern is returned if the negative lookahead part is not present.

41
Q

Visualise an illustration of regex’ positive and negative lookaheads.

A
42
Q

Visualise a regex patern that creates a simple password checker that looks for between 3 and 6 characters and at least one number.

A
43
Q

Use lookaheads in the pwRegex to match passwords that are greater than 5 characters long, and have two consecutive digits.

A
44
Q

In regex, how do you check for mixed groupings of characters?

A
45
Q

Fix the regex so that it checks for the names of Franklin Roosevelt or Eleanor Roosevelt in a case sensitive manner and it should make concessions for middle names.

Then fix the code so that the regex that you have created is checked against myString and either true or false is returned depending on whether the regex matches.

A
46
Q

In regex how can you reuse paterns using capture groups?

A

You could use /row row row/, but what if you don’t know the specific word repeated? Capture groups can be used to find repeated substrings.

47
Q

Use capture groups in reRegex to match a string that consists of only the same number repeated exactly three times separated by single spaces.

A
48
Q

In regex, how can you search and replace?

A

You can search and replace text in a string using .replace() on a string. The inputs for .replace() is first the regex pattern you want to search for. The second parameter is the string to replace the match or a function to do something.

49
Q

Write a regex fixRegex using three capture groups that will search for each word in the string one two three. Then update the replaceText variable to replace one two three with the string three two one and assign the result to the result variable. Make sure you are utilizing capture groups in the replacement string using the dollar sign ($) syntax.

A
50
Q

Write a regex and use the appropriate string methods to remove whitespace at the beginning and end of the strings

” Hello, World! “.

A
51
Q
A