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 ____

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 ___

25
\w shortcut is equal to
[A-Za-z0-9_] Note, this character class also includes the underscore character (_).
26
You can search for the opposite of the \w with
\W Note, the opposite pattern uses a capital letter. This shortcut is the same as [^A-Za-z0-9_]
27
the shortcut for digits is
\d this is equal to [0-9]
28
the opposite of digits is
\D this is equal to [^0-9]
29
match whitespace
\s also matches carriage return, tab, form feed, and new line characters
30
opposite of whitespace
\S
31
specify the lower and upper number of patterns with quantity specifiers
{ 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
To only specify the lower number of patterns,
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
specify a certain number of patterns
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
specify the possible existence of an element with 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
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.
36
There are two kinds of lookaheads:
positive lookahead and negative lookahead.
37
A positive lookahead
will look to make sure the element in the search pattern is there, but won't actually match it.
38
A positive lookahead is used as
(?=...) where the ... is the required part that is not matched.
39
a negative lookahead will look to make sure the element in the search pattern is
not there
40
A negative lookahead is used as
(?!...) where the ... is the pattern that you do not want to be there.
41
The rest of the pattern is _____ if the negative lookahead part is not present.
returned
42
A more practical use of lookaheads is
to check two or more patterns in one string
43
check for groups of characters
using ()
44
search for repeat substrings
capture groups
45
To specify where that repeat string will appear
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
capture just 3 repeating patterns of digits separated by space
``` let repeatNum = "42 42 42"; let reRegex = /^(\d+)(\s)\1\2\1$/; // Change this line let result = reRegex.test(repeatNum); ```
47
ou can search and replace text in a string using
.replace()
48
The inputs for .replace() is first _____. The second parameter is ____
(1) the regex pattern you want to search for | (2) the string to replace the match
49
You can also access capture groups in the replacement string with _____ like _____
dollar signs $ like this ``` "Code Camp".replace(/(\w+)\s(\w+)/, '$2 $1'); // Returns "Camp Code" ```
50
How to replicate String.prototype.trim() in regex (code)
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");