RegEx Flashcards

1
Q

what the test() method does

A

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);
// Returns true

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

search for multiple patterns using the

A

alternation or OR operator: |

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

match both cases using

A

a 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
4
Q

extract actual matches with ___

A

match() method

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

To search or extract a pattern more than once…

A

use the g flag

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

what the wildcard character . does

A

will match any one character

if you wanted to match “hug”, “huh”, “hut”, and “hum”, you can use the regex /hu./ to match all four words

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

what are character classes

A

Character classes allow you to define a group of characters you wish to match by placing them inside square [ ] brackets.

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

match “bag”, “big”, and “bug” but not “bog”

A

/b[aiu]g/

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

define a range of characters to match

A

[a-e] finds characters a to e

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

/[0-5]/ matches

A

any number between 0 and 5 including 0 and 5

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

matches all letters and numbers in word

A

/[a-z0-9]/ig

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

create a negated character set with

A

you place a caret character (^) after the opening bracket and before the characters you do not want to match.

/[^aeiou]/gi matches all characters that are not a vowel

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

how to match a character (or group of characters) that appears one or more times in a row

A

use the + character

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

find matches when the letter s occurs one or more times in “Mississippi”

A

/s+/g

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

option that matches characters that occur zero or more times

A

asterisk or star: *

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

greedy vs lazy match

A

a greedy match finds the longest possible part of a string

a lazy match finds the smallest possible part of the string

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

Regular expressions are by default {greedy or lazy?)

A

greedy

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

you can use the ____ character to change it to lazy matching

A

?

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

“titanic” matched against the adjusted regex of /t[a-z]*?i/ returns

A

[“ti”]

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

“titanic” matched against the adjusted regex of /t[a-z]*i/ returns

A

[“titani”]

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

Parsing HTML with regular expressions should be ____

A

avoided

22
Q

Outside of a character set, the caret (^) is used to search for patterns at

A

the beginning of strings.

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

23
Q

You can search the end of strings using _____ at the end of the regex

A

the dollar sign character $

24
Q

The closest character class in JavaScript to match the alphabet is ___

A

\w

25
Q

\w shortcut is equal to

A

[A-Za-z0-9_]

Note, this character class also includes the underscore character (_).

26
Q

You can search for the opposite of the \w with

A

\W

Note, the opposite pattern uses a capital letter. This shortcut is the same as [^A-Za-z0-9_]

27
Q

the shortcut for digits is

A

\d

this is equal to [0-9]

28
Q

the opposite of digits is

A

\D

this is equal to [^0-9]

29
Q

match whitespace

A

\s

also matches carriage return, tab, form feed, and new line characters

30
Q

opposite of whitespace

A

\S

31
Q

specify the lower and upper number of patterns with quantity specifiers

A

{ and }

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 ohStr = "Ohhh no";
let ohRegex = /Oh{3,6} no/; 
// would match Oh, Ohh, Ohhh, Ohhhh, Ohhhhh, Ohhhhhh
let result = ohRegex.test(ohStr);
32
Q

To only specify the lower number of patterns,

A

keep the first number followed by a comma.

{3,}

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

33
Q

specify a certain number of patterns

A

just have that one number between the curly brackets.

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

34
Q

specify the possible existence of an element with a

A

?

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

Lookaheads are patterns that tell JavaScript

A

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.

36
Q

There are two kinds of lookaheads:

A

positive lookahead and negative lookahead.

37
Q

A positive lookahead

A

will look to make sure the element in the search pattern is there, but won’t actually match it.

38
Q

A positive lookahead is used as

A

(?=…) where the … is the required part that is not matched.

39
Q

a negative lookahead will look to make sure the element in the search pattern is

A

not there

40
Q

A negative lookahead is used as

A

(?!…) where the … is the pattern that you do not want to be there.

41
Q

The rest of the pattern is _____ if the negative lookahead part is not present.

A

returned

42
Q

A more practical use of lookaheads is

A

to check two or more patterns in one string

43
Q

check for groups of characters

A

using ()

44
Q

search for repeat substrings

A

capture groups

45
Q

To specify where that repeat string will appear

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.

46
Q

capture just 3 repeating patterns of digits separated by space

A
let repeatNum = "42 42 42";
let reRegex = /^(\d+)(\s)\1\2\1$/; // Change this line
let result = reRegex.test(repeatNum);
47
Q

ou can search and replace text in a string using

A

.replace()

48
Q

The inputs for .replace() is first _____. The second parameter is ____

A

(1) the regex pattern you want to search for

(2) the string to replace the match

49
Q

You can also access capture groups in the replacement string with _____ like _____

A

dollar signs $

like this

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

How to replicate String.prototype.trim() in regex (code)

A

let hello = “ Hello, World! “;
let wsRegex = /(\s+)(\w+,\s\w+!)(\s+)/; // create 3 groups, 2 for whitespace at both ends, and one in the middle for words - and keep it
let result = hello.replace(wsRegex,”$2”);