Regular Expressions Flashcards

1
Q

Name the String methods that work with RegEx patterns:

A

match, replace, search, split

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

Make pattern for finding all numbers that are two or more digits (e.g. will find 10 and 333 but not 3 in “3 333 10”)

A

/\d{ 2, }/g – escape d; comma says a number to or more digits.

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

Make a pattern for finding the first letter in every word of a sentence.

A

/^[a-zA-Z]|\s[a-zA-Z]/g; ^ says find any letter at start of sentence; | says ‘or’ and \s says whitespace preceeding any letter.

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

what are the RegEx flags supported by js?

A

g for global, i for ignore case, and m for multiline

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

What is the literal for finding if a string contains 3 digits in a row?

A

/\d{3}/;
or /[0-9][0-9][0-9]/
Note: this will be true for “123” as well as “1234” but not “12”

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

If you simply want to know if a string has an @ with characters on either side, do what?

A

mystring.search( /[a-zA-Z]@[a-zA-Z]/ ); // returns -1 if nothing found; otherwise, returns index of first location.

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

what is the syntax for the replace() method?

A

nameOfString.replace(patternToMatch, replaceWith);

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

What does the question mark do?

A

Preceding character is optional. Matches 0 or 1 occurrence. Pattern colou?r will match color or colour. Pattern belle? will match bell or belle.

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

What does [XYZ] do?

A

Matches any single character from the character class. [ch]at will match cat and hat but not at.

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

Match a pattern only if it occurs at beginning of string with what character?

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

Match a pattern only if it occurs at end of string with what character?

A

$

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

Match anything that’s NOT a letter (not a, b, c, A, B, etc)

A

[^a-zA-Z]

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

Match the word ‘hello’ ONLY if it’s the only content in a string. Match “hello” but not “ hello” or “yo, hello”.

A

/^hello$/

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

Search for the words “image” or alttext” or “classname”

A

/image|alttext|classname/g

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

What arguments does the string.replace() function take and how do they work?

A

Arg1 is the pattern. Arg2 is the replacement string or else a function that takes the match (and additional arguments as well) and returns a string.

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

What are the regexp methods?

A

pattern.exec(someString) and pattern.test(someString)

17
Q

Use a regExp method to find if the variable txt contains ‘cat’

A

/cat/gi.test(txt);