Regular Expressions Flashcards

1
Q

How do you use a regular expression to determine if a string contains text?

A

One way to test a regex is using the .test() method. 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.

let testStr = “freeCodeCamp”;
let testRegex = /Code/;
testRegex.test(testStr);

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

Are regular expressions case sensitive?

A

Yes

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

What’s the syntax for searching for a string for multiple patterns?

A

This is powerful to search single strings, but it’s limited to only one pattern. You can search for multiple patterns using the alternation or OR operator: |.

This operator matches patterns either before or after it. For example, if you wanted to match “yes” or “no”, the regex you want is /yes|no/.

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

How do you ignore case while searching a string?

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.

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

How do you extract matches from a string?

A

To use the .match() method, apply the method on a string and pass in the regex inside the parentheses.

Here’s an example:

"Hello, World!".match(/Hello/);
// Returns ["Hello"]
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How do you extract or match multiple patterns in a string?

A

To search or extract a pattern more than once, you can use the g flag.

let repeatRegex = /Repeat/g;
testStr.match(repeatRegex);

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

What is the wildcard character and what does it do?

A

The wildcard character . will match any one character. The wildcard is also called dot and period.

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

How do search a pattern for multiple possibilities?

A

ou can search for a literal pattern with some flexibility with character classes. Character classes allow you to define a group of characters you wish to match by placing them inside square ([ and ]) brackets.

For example, you want to match “bag”, “big”, and “bug” but not “bog”.

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

How do you search a range of characters?

A

Inside a character set, you can define a range of characters to match using a hyphen character: -.

For example, to match lowercase letters a through e you would use [a-e].

For example, /[0-5]/ matches any number between 0 and 5, including the 0 and 5.

Also, it is possible to combine a range of letters and numbers in a single character set.

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

How do you create a set of characters that you don’t want to match?

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
11
Q

How do you match characters one or more times?

A

This means it occurs at least once, and may be repeated.

You can use the + character to check if that is the case. Remember, the character or pattern has to be present consecutively. That is, the character has to repeat one after the other.

For example, /a+/g would find one match in “abc” and return [“a”]. Because of the +, it would also find a single match in “aabc” and return [“aa”].

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

How do you match characters that occur zero or more times?

A

There’s also an option that matches characters that occur zero or more times.

The character to do this is the asterisk or star: *.

let soccerWord = "gooooooooal!";
let gPhrase = "gut feeling";
let oPhrase = "over the moon";
let goRegex = /go*/;
soccerWord.match(goRegex); // Returns ["goooooooo"]
gPhrase.match(goRegex); // Returns ["g"]
oPhrase.match(goRegex); // Returns null
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Do you regular expressions by default find the longest or shortest possible match?

A

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
14
Q

How do you find the shortest possible match?

A

However, 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
15
Q

How do you search for patterns at the beginning of strings?

A

Outside of a character set, the caret is used to search for patterns at the beginning of strings.

let firstString = “Ricky is first and can be found.”;
let firstRegex = /^Ricky/;
firstRegex.test(firstString);

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

How do you 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.

let theEnding = “This is a never ending story”;
let storyRegex = /story$/;
storyRegex.test(theEnding);
// Returns true
let noEnding = “Sometimes a story will have to end”;
storyRegex.test(noEnding);
// Returns false

17
Q

How do you match all letters and numbers in a string?

A
The closest character class in JavaScript to match the alphabet is \w. This shortcut is equal to [A-Za-z0-9_]. 
This character class matches upper and lowercase letters plus numbers. Note, this character class also includes the underscore character (_).
let longHand = /[A-Za-z0-9_]+/;
let shortHand = /\w+/;
18
Q

What’s the shortcut to look for digit characters?

A

The shortcut to look for digit characters is \d, with a lowercase d. This is equal to the character class [0-9].

19
Q

What’s the shortcut to match all non-numbers?

A

ou can also search for non-digits using a similar shortcut that uses an uppercase D instead.

The shortcut to look for non-digit characters is \D. This is equal to the character class [^0-9], which looks for a single character that is not a number between zero and nine.

20
Q

How do you match whitespace and system characters in a string?

A

You can search for whitespace using \s, which is a lowercase s. This pattern not only matches whitespace, but also 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].

21
Q

How do you search for non-whitespace in a string?

A

Search for non-whitespace using \S, which is an uppercase s. This pattern will not match whitespace, carriage return, tab, form feed, and new line characters. You can think of it being similar to the character class [^ \r\t\f\n\v].

22
Q

How do you specify the lower and upper number of patterns to match?

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

let A4 = "aaaah";
let A2 = "aah";
let multipleA = /a{3,5}h/;
multipleA.test(A4); // Returns true
multipleA.test(A2); // Returns false
23
Q

How do you search for only the lower number of matches in a string?

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

let A4 = "haaaah";
let A2 = "haah";
let A100 = "h" + "a".repeat(100) + "h";
let multipleA = /ha{3,}h/;
multipleA.test(A4); // Returns true
multipleA.test(A2); // Returns false
multipleA.test(A100); // Returns true
24
Q

How do you search for an exact number of matches in a string?

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

25
Q

How do you check for 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.

let american = "color";
let british = "colour";
let rainbowRegex= /colou?r/;
rainbowRegex.test(american); // Returns true
rainbowRegex.test(british); // Returns true
26
Q

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

let quit = “qu”;
let quRegex= /q(?=u)/;
quit.match(quRegex); // Returns [“q”]

27
Q

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

let noquit = “qt”;
let qRegex = /q(?!u)/;
noquit.match(qRegex); // Returns [“q”]

28
Q

How do you check for a mixed group of characters?

A

Sometimes we want to check for groups of characters using a Regular Expression and to achieve that we use parentheses ().

If you want to find either Penguin or Pumpkin in a string, you can use the following Regular Expression: /P(engu|umpk)in/g

29
Q

How do you search for repeated substrings?

A

You can search for repeat substrings using capture groups. Parentheses, ( and ), are used to find repeat substrings. You put the regex of the pattern that will repeat in between the parentheses.

To specify where that repeat string will appear, you use a backslash () and then a number. This number starts at 1 and increases with each additional capture group you use. An example would be \1 to match the first group.

30
Q

How do you change the text you match?

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.

let wrongText = “The sky is silver.”;
let silverRegex = /silver/;
wrongText.replace(silverRegex, “blue”);
// Returns “The sky is blue.”

You can also access capture groups in the replacement string with dollar signs ($).

"Code Camp".replace(/(\w+)\s(\w+)/, '$2 $1');
// Returns "Camp Code"
31
Q

How do you remove whitespace at the beginning and end of a string?

A

/^\s+|\s+$/g