Regex Flashcards

1
Q

see if the word ‘bobcat’ has an ‘o’ in it

A

> /o/.test(‘bobcat’)
= true

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

see if the word ‘bobcat’ has an ‘l’ in it

A

> /l/.test(‘bobcat’)
= false

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

What’s the difference between .test and .exec?

A

the exec() method will return the specified match if present or null if not present in the given string whereas the test() method returns a Boolean result

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

How would you capture the char after ‘the’ in ‘The big dog jumps’?

A

let string ‘The big dog jumps’;
let regex= /[tT]he(.)/g;
let result = regex.exec(string)

//will capture the char after ‘The’

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

How do we create a capture group?

A

using ().

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

How do we name a capture group? and then use that capture group

A

using ?<name> at the beginning of our group definition</name>

let namedRegex = /[Tt]he(?<charAfterThe>.)/g
let namedResult = namedRegex.exec(string)
let charAfter = namedResult.groups.charAfterThe</charAfterThe>

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

how would you match all 4 or 5 letter words

A

/\w{4,5}/

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

match any word after the word ‘the’

A

/(?<=[tT]he )\w+/g

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

match any word before the word ‘the’

A

/\w*(?= [tT]he)/g

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

+

A

matches one or more in a row
ie /e+/g will find all ‘e’ or multiple ‘e’ substrings

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

?

A
  • optional
  • ie /e+a?/g will find all ‘e’ or ‘ee’ or ‘ea’ substrings
  • also changes * when chained ?. makes the behavior non-greedy. repetition will first try to match asfew*reps as possible
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

*

A
  • match 0 or more
  • ie /re*/g will find all ‘r’ or ‘re’ or ‘ree’
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

.

A

matches anything except a newline

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

\

A

escape char
if you need to seach say for a period you can say /./g

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

\w

A

matches any word char, so any letter

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

\W

A

will match anything thats NOT a word char

17
Q

\s

A

will match any white space

18
Q

\S

A

matches anything that is not white space

19
Q

\d

A

matches any number digit

19
Q

\b

A

enforces a word break

function hasThanks(str) {
return /\b(thanks|thank you)\b/i.test(str);
}

hasThanks(“Thanks! You helped me a lot.”); // true
hasThanks(“Just want to say thank you for all your work.”); // true
hasThanks(“Thanksgiving is around the corner.”); // false

20
Q

{}

A
  • specifies the number of chars
  • /\w{4}/ will get any 4 letter words
  • /\w{4,6}/ will get any words between 4 and 6 letters
21
Q

[]

A
  • matches any chars inside the brackets
  • /[ft]at/g will find all 3 letter words starting with f or t and ending in at.
  • words with ranges [a-z] or [0-9A-Z]
22
Q

()

A
  • allows you to create groupings
  • anything after the grouping will affect the whole grouping ie /(t|e|r){2,3}/ will capture any 2 to 3 character long string of t, r, and e
  • these groups are also called capture groups and allow you to ‘capture’ each group to modify it later
  • (t|T) allows you to match lower case t or upper caseT
  • (t|T)he matches any occurrence of ‘the’ or ‘The’
  • (re){2,3} will find any instances of ‘rere’ or ‘rerere’
23
Q
A
  • match only to the beginning of the line.
  • ^T will find capital T at the beginning of the line
  • WHEN USE INSIDE A CHAR GROUP CAN ALSO BE USED TO REPRESENT NOT
    • ie [^s] means match any char that isnt S
24
Q

$

A

matches to the end of the statement