Regex Flashcards

1
Q

What are the two ways to create regular expressions in JS?

A
Constructor: new RegExp(pattern[,flags])
e.g.
let regex = new RegExp('abc','i');

Literal: /pattern/flags
e.g.
let regex = /abc/i;

Both expressions would match against a sequence of a then b then c in a string ignoring case e.g. Abcre, baBC etc.

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

I want to know if the word ‘lite’ appears in a sentence and get a simple true/false response.

What regular expression method would I use?

A

test

e.g.

let regex = /lite/
let sentence = ‘ no such lite light’
let isPresent = regex.test(sentence)
isPresent = true;

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

How would I search the string ‘the quick brown Fox’ for the letter ‘f’ no matter the case?

A

let regex = /f/i

regex.test(‘the quick brown Fox’)

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

How would I get an array of all the capital letters in the sentence “The capital of Scotland is Edinburgh”.

A
let regex = /[A-Z]/g  //all capital letters, do not stop at first match.
let result = "The capital of Scotland is Edinburgh.".match(regex);
result = ['T', 'S', 'E' ];
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How would the result change if I removed the global flag from this regular expression operation?

let result = “17 Mall Road”.match(/\d/g);

A

The result of “17 Mall Road”.match(/\d/g) is [“1”, “7”].

Without the global flag it would only return the first match i.e. the number 1.

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

Write a regular expression that matches words containing bar and car.

A

/b|car/
or
/[bc]ar/

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

What is a character set?

A

A character set is a way to match different characters in a single position. It matches any string that contains any of the characters within the brackets

e.g. /[bcd]/

would match anyword containing b, c or d..

It is similar to or i.e. (b | c | d)

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

What are the different ways that the carat symbol ^ can be used in regular expressions?

A
  1. At the beginning of a character set, within the brackets e.g. /[^kl]e/

Here it means NOT k, NOT L. It matches anything NOT included in the character set.

  1. At the beginning of the regex proceeding the regex. e.g. /^p/

Here it means the string must start with p and will match ‘poll’ but not ‘nap’, for example.

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

Write a regular expression to find lowercase letters.

A

/[a-z]/g

This is a range. Instead of writing [abcdefg…etc.], you can use this shorthand.

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

Write a regular expression to find any digit character.

A

/[0-9]/
OR
/\d/

\d is a meta character, a shorthand for writing [0-9]

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

What will the following regex match?

/\s/

A

Any whitespace characters e.g. spaces, tabs

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

What is a word character?

Write a regular expression to find any word characters?

A

A word character is any alphanumeric character (letters and digits) in addition to the underscore i.e.

/[a-zA-Z0-9_]/
or
/\w/

\w is a meta character

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

Write a regular expression to match any non digit character.

A

/[^0-9]/
OR
/\D/

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

Write a regular expression to match any non word character.

A

/[^a-zA-Z0-9]/
OR
/\W/

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

What meta character matches any character except for a new line?

A

full stop i.e. .

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

Write a regular expression to match a tab character only.

A

/\t/

17
Q

Write a regular expression to match any non space character.

A

/\S/

18
Q

What does this mean?

const regex = /\d+/;

A

Match any string containing at least one but possibly more than one digit.

+ is a quantifier and matches the preceding expression one or more times.

19
Q

What does this match?

const regex = /go*d/;

A

Matches ‘gd’ and ‘god’ and ‘gooood’ i.e. any word that contains g and d and any number of ‘o’s in the middle (possibly zero).

  • is a quantifier and matches the preceding expression 0 or more times.
20
Q

What does this mean? What does it match?

const regex = /goo?d/;

A

Matches ‘god’ and ‘good’ but not ‘goood’.

? is a quantifier and matches the preceding expression 0 or 1 time only.

21
Q

How is the quantifier ‘$’ used in a regular expression?

A

Anything preceding $ should match the end of the string

e.g. const regex = /.com$/

This matches any string ending in ‘com’.

22
Q

What does this match?

/go{2}d/

A

It matches only ‘good’

{N} is a quantifier and matches exactly N occurences of the preceding expression.

23
Q

What does this match?

/go{2,}d/

A

It matches words containing a string starting with g and ending with d with at least two ‘o’s in between e.g. ‘good’, ‘gooood’ etc. but not ‘god’.

{N,} is a quantifier and matches AT LEAST N occurences of the preceding expression

24
Q

What does this match?

/go{1,2}d/

A

It matches ‘god’ and ‘good’ only.

{N,M} is a quantifier and matches at least N and AT MOST M occurences of the preceding expression.

25
Q

What’s the difference between:

/a+b/
and
/a+b/

A

/a+b/ will match any strings with one or more ‘a’s finishing with b e.g. ‘ab’, ‘aab’ but not ‘b’ and not ‘a+b’.

/a+b/ will match the string ‘a+b’

Here \ is used to escape the special character ‘+’

26
Q

Write a regular expression to match any 10 numbers.

A

/^\d{10}$/

Use ^ and $ to enforce that the match should span the whole string i.e. the string cannot be more than 10 characters.

Use \d to identify digits.

Use the quantifier {10} to specify exactly 10 digits

27
Q

Write a regular expression to match a data with the format DD-MM-YYYY or DD-MM-YY.

A

/^(\d{2}-){2}-\d{2}(\d{2})?$/

28
Q

Write a regular expression to match with a format like abc.def.ghi.jkl (that exact number of dots) where each variable a, b, c, d, e, f, g, h, i, j, k, l can be any character except new line

A

/^(.{3}.){3}.{3}$/

Use ^ and $ to enforce that the match should span the whole string i.e. be made up of 15 characters.

Use . to identify any character except a new line and . to identify a dot/full stop.

Use the quantifier {3} to match an expression 3 times.

29
Q

Write a function to return the number of vowels in a string using regex.

A

const vowels = str => str.match(/[aeiou]/ig)?.length ?? 0

Match returns null if no match found or an array of all the matches.

30
Q

Write a function to capitalize the first letter of every word in a sentence.

A

const capitalize = sentence => sentence.split(/\s/g).map(word => word.substring(0,1).toUpperCase() + word.substring(1)).join(“ “)