JavaScript Regex Mastery Quiz Guide
JavaScript Regex Mastery Quiz Guide
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses [Link]
1
Examples: 9
Matching Email Addresses: 9
Extracting Dates from Text: 9
Replacing Text: 9
Advanced Regular Expression Features: 10
Grouping ( ): 10
Capturing Groups ( ): 10
Non-capturing Groups (?: ): 10
Assertions: 10
Positive Lookahead (?= ): 11
Negative Lookahead (?! ): 11
Quantifiers and Lazy Matching: 11
Validating URLs: 11
Extracting HTML Tags: 11
Validating Passwords: 12
Parsing CSV: 12
Replacing Words with Callback: 12
Coding Exercise Regex 13
Exercise 1: Validate Email Address 13
Exercise 2: Extract Numbers from String 14
Exercise 3: Replace HTML Tags 15
Exercise 4: Validate Password Strength 16
Exercise 5: Extract Domain from URL 17
Exercise 6: Validate Date Format 18
Exercise 7: Extract Hashtags from Text 19
Exercise 8: Validate Credit Card Number 20
Exercise 9: Replace URLs with Links 22
Exercise 10: Validate Phone Number 23
Regex Quiz Questions 24
Question: What is a regular expression in JavaScript? 29
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses [Link]
2
Question: Which object in JavaScript is used for working with regular
expressions? 29
Question: What does the test() method of a RegExp object do? 29
Question: Which character is used as the delimiter for a regular expression
literal? 30
Question: What does the exec() method of a RegExp object return? 30
Question: Which quantifier matches zero or more occurrences of the preceding
element? 30
Question: What is the purpose of the ^ anchor in a regular expression? 31
Question: Which character class matches any digit character? 31
Question: In the regular expression /ab+c/, what does the + symbol represent?
31
Question: What is the purpose of the non-capturing group (?: ) in a regular
expression? 32
Question: What does the ? quantifier indicate in a regular expression? 32
Question: Which assertion checks if a certain element is followed by another
element without including the latter in the match? 32
Question: What is the purpose of the g flag in a regular expression? 33
Question: Which character is used for matching any character except a
newline? 33
Question: In the regular expression /[aeiou]/, what does the character class
[aeiou] represent? 33
Question: Which quantifier matches exactly three occurrences of the preceding
element? 34
Question: What does the replace() method do when used with a regular
expression? 34
Question: Which method is used to find all occurrences of a pattern in a string,
including capturing groups? 34
Question: In the regular expression /^[\w.-]+@[a-z]+\.[a-z]+$/i, what does the i
flag represent? 35
Question: What is the purpose of the \b anchor in a regular expression? 35
Question: Which method returns an array of all occurrences of a pattern in a
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses [Link]
3
string without capturing groups? 35
Question: In the regular expression /[^0-9]/, what does the character class
[^0-9] represent? 36
Question: Which quantifier matches between two and four occurrences of the
preceding element? 36
Question: What does the \d character class represent in a regular expression?
36
Question: In the regular expression /^\d{3}-\d{2}-\d{4}$/, what does the
pattern represent? 37
Question: What does the matchAll() method return? 37
Question: What does the \S character class represent in a regular expression?
37
Question: In the regular expression /(\w+)\s(\w+)/, what do the capturing
groups (\w+) represent? 38
Question: Which character is used to escape a metacharacter in a regular
expression? 38
Question: What is the purpose of the search() method in JavaScript when used
with a regular expression? 38
Question: Which method splits a string into an array of substrings based on a
regular expression? 39
Question: What is the purpose of the \w character class in a regular
expression? 39
Question: In the regular expression /(\d{2})\/(\d{2})\/(\d{4})/, what do the
capturing groups (\d{2}), (\d{2}), and (\d{4}) represent? 39
Question: What is the purpose of the \W character class in a regular
expression? 40
Question: In the regular expression /(\b\w+\b)\s\1/, what does \1 represent?
40
Question: What does the \b anchor do in a regular expression? 40
Question: Which quantifier matches one or more occurrences of the preceding
element? 41
Question: In the regular expression /^[A-Z][a-z]*$/, what does the pattern
represent? 41
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses [Link]
4
Question: What is the purpose of the \n escape sequence in a regular
expression? 41
Question: In the regular expression /(\d+)\s(?:years?|yrs?)/, what does
(?:years?|yrs?) represent? 42
Question: What is the purpose of the \s character class in a regular expression?
42
Question: In the regular expression /(\d{3})-(\d{2})-(\d{4})/, what does the
capturing group (\d{3}) represent? 42
Question: Which method is used to test if a string contains a pattern in a
regular expression? 43
Question: What does the \S+ pattern represent in a regular expression? 43
Question: In the regular expression /^[a-zA-Z]\w*$/, what does the pattern
represent? 43
Question: What is the purpose of the \ character in a regular expression? 44
Question: In the regular expression /[^aeiou]/, what does the character class
[^aeiou] represent? 44
Question: What does the flags property of a RegExp object return? 44
Question: What is the purpose of the $ anchor in a regular expression? 45
Question: In the regular expression /([a-z]+)\s(\d+)/, what do the capturing
groups ([a-z]+) and (\d+) represent? 45
Quiz Answers 45
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses [Link]
5
Basics of Regular Expressions:
Metacharacters:
Regular expressions consist of ordinary characters (like letters
and numbers) and metacharacters that have special meaning.
Some common metacharacters include . (any character), ^ (start
of a line), $ (end of a line), * (zero or more occurrences), + (one
or more occurrences), and ? (zero or one occurrence).
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses [Link]
6
Regular Expression Methods:
test():
The test() method checks if a pattern exists in a string and
returns true or false.
let regex = /world/;
let result = [Link]("Hello, world!"); // true
exec():
The exec() method searches for a match in a string. It returns an
array containing the matched text and additional information or
null if no match is found.
let regex = /world/;
let result = [Link]("Hello, world!"); // ["world", index: 7,
input: "Hello, world!", groups: undefined]
Character Classes:
[] - Character Set:
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses [Link]
7
[^] - Negated Character Set:
Match any character that is not in the specified set.
let regex = /[^0-9]/;
Quantifiers:
{n}:
Matches exactly n occurrences of the preceding character or
group.
let regex = /a{3}/;
{n,}:
Matches n or more occurrences of the preceding character or
group.
let regex = /\d{2,}/; // Match two or more digits
{n,m}:
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses [Link]
8
Examples:
Replacing Text:
let text = "Hello, my name is John.";
let nameRegex = /John/;
let updatedText = [Link](nameRegex, "Doe");
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses [Link]
9
Advanced Regular Expression Features:
Grouping ( ):
Parentheses are used for grouping parts of a pattern together.
let regex = /(ab)+/;
Capturing Groups ( ):
Capturing groups allow you to extract matched parts of a pattern.
let dateRegex = /(\d{1,2})\/(\d{1,2})\/(\d{4})/;
let dateString = "Today's date is 12/25/2023.";
let dateMatch = [Link](dateRegex);
// dateMatch: ["12/25/2023", "12", "25", "2023"]
Assertions:
Assertions are conditions that must be true for a match to occur.
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses [Link]
10
Positive Lookahead (?= ):
let regex = /Java(?=Script)/;
Validating URLs:
Check if a string is a valid URL.
let urlRegex = /^(https?|ftp):\/\/[^\s/$.?#].[^\s]*$/;
let isValidURL = [Link]("[Link]
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses [Link]
11
let tags = [Link](htmlRegex);
Validating Passwords:
Ensure a password meets specific criteria.
let passwordRegex =
/^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d@$!%*?&]{8,}$/;
let isValidPassword = [Link]("Passw0rd");
Parsing CSV:
Extract values from a comma-separated values string.
let csvString = "John,Doe,30\nAlice,Smith,25";
let csvRegex = /([^,\n]+)(,|$)/g;
let csvMatches = [...[Link](csvRegex)];
// csvMatches: [["John", ","], ["Doe", ","], ["30", "\n"], ["Alice",
","], ["Smith", ","], ["25", ""]]
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses [Link]
12
let updatedText = [Link](regex, (match, word) =>
[Link]());
// updatedText: "HELLO WORLD, THIS IS AWESOME."
Steps:
1. Define a regular expression for validating email addresses.
2. Write a function that uses the regular expression to check if
the input is a valid email address.
Code Example:
function isValidEmail(email) {
// Regular expression for email validation
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses [Link]
13
let emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
return [Link](email);
}
Solution:
function isValidEmail(email) {
let emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
return [Link](email);
}
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses [Link]
14
let numberRegex = /\d+/g;
return [Link](numberRegex);
}
Steps:
1. Define a regular expression to match HTML tags.
2. Write a function that uses replace to remove all HTML tags.
Code Example:
function removeHtmlTags(inputString) {
// Regular expression to match HTML tags
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses [Link]
15
let htmlTagRegex = /<[^>]*>/g;
return [Link](htmlTagRegex, '');
}
function isValidPassword(password) {
// Regular expression for password validation
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses [Link]
16
let passwordRegex =
/^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d@$!%*?&]{8,}$/;
return [Link](password);
}
Steps:
1. Define a regular expression to match the domain part of a
URL.
2. Write a function that uses match to extract the domain.
Code Example:
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses [Link]
17
function extractDomain(url) {
// Regular expression to match domain part of a URL
let domainRegex =
/^(?:https?:\/\/)?(?:[^@\/\n]+@)?(?:www\.)?([^:\/\n]+)/;
let match = [Link](domainRegex);
return match ? match[1] : null;
}
Steps:
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses [Link]
18
1. Define a regular expression for validating the date format.
2. Write a function that uses the regular expression to check if
the input is a valid date.
Code Example:
function isValidDateFormat(dateString) {
// Regular expression for MM/DD/YYYY format
let dateFormatRegex =
/^(0[1-9]|1[0-2])\/(0[1-9]|[12][0-9]|3[01])\/\d{4}$/;
return [Link](dateString);
}
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses [Link]
19
Steps:
1. Define a regular expression to match hashtags.
2. Write a function that uses match to extract all hashtags from
the input text.
Code Example:
function extractHashtags(text) {
// Regular expression to match hashtags
let hashtagRegex = /\B#\w+/g;
return [Link](hashtagRegex);
}
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses [Link]
20
Steps:
1. Define a regular expression for validating credit card
numbers.
2. Write a function that uses the regular expression to check if
the input is a valid credit card number.
Code Example:
function isValidCreditCardNumber(cardNumber) {
// Regular expression for credit card validation (simple example)
let creditCardRegex = /^\d{4}-\d{4}-\d{4}-\d{4}$/;
return [Link](cardNumber);
}
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses [Link]
21
Exercise 9: Replace URLs with Links
Description: Write a function that replaces all URLs in a given text
with clickable links.
Steps:
1. Define a regular expression to match URLs.
2. Write a function that uses replace to replace URLs with HTML
links.
Code Example:
function replaceUrlsWithLinks(text) {
// Regular expression to match URLs
let urlRegex = /https?:\/\/\S+/g;
return [Link](urlRegex, (url) => `<a href="${url}"
target="_blank">${url}</a>`);
}
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses [Link]
22
Solution:
function replaceUrlsWithLinks(text) {
let urlRegex = /https?:\/\/\S+/g;
return [Link](urlRegex, (url) => `<a href="${url}"
target="_blank">${url}</a>`);
}
Steps:
1. Define a regular expression for validating phone numbers.
2. Write a function that uses the regular expression to check if
the input is a valid phone number.
Code Example:
function isValidPhoneNumber(phoneNumber) {
// Regular expression for phone number validation (simple
example)
let phoneRegex = /^\d{3}-\d{3}-\d{4}$/;
return [Link](phoneNumber);
}
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses [Link]
23
[Link](isValidPhoneNumber("123-456-7890")); // true
[Link](isValidPhoneNumber("invalid-phone-number")); //
false
Solution:
function isValidPhoneNumber(phoneNumber) {
let phoneRegex = /^\d{3}-\d{3}-\d{4}$/;
return [Link](phoneNumber);
}
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses [Link]
24
5. Question: What does the exec() method of a RegExp object
return?
6. Question: Which quantifier matches zero or more
occurrences of the preceding element?
7. Question: What is the purpose of the ^ anchor in a regular
expression?
8. Question: Which character class matches any digit
character?
9. Question: In the regular expression /ab+c/, what does the +
symbol represent?
10. Question: What is the purpose of the non-capturing group
(?: ) in a regular expression?
11. Question: What does the ? quantifier indicate in a regular
expression?
12. Question: Which assertion checks if a certain element is
followed by another element without including the latter in
the match?
13. Question: What is the purpose of the g flag in a regular
expression?
14. Question: Which character is used for matching any
character except a newline?
15. Question: In the regular expression /[aeiou]/, what does
the character class [aeiou] represent?
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses [Link]
25
16. Question: Which quantifier matches exactly three
occurrences of the preceding element?
17. Question: What does the replace() method do when used
with a regular expression?
18. Question: Which method is used to find all occurrences of
a pattern in a string, including capturing groups?
19. Question: In the regular expression
/^[\w.-]+@[a-z]+\.[a-z]+$/i, what does the i flag
represent?
20. Question: What is the purpose of the \b anchor in a
regular expression?
21. Question: Which method returns an array of all
occurrences of a pattern in a string without capturing
groups?
22. Question: In the regular expression /[^0-9]/, what does
the character class [^0-9] represent?
23. Question: Which quantifier matches between two and four
occurrences of the preceding element?
24. Question: What does the \d character class represent in a
regular expression?
25. Question: In the regular expression
/^\d{3}-\d{2}-\d{4}$/, what does the pattern represent?
26. Question: What does the matchAll() method return?
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses [Link]
26
27. Question: What does the \S character class represent in a
regular expression?
28. Question: In the regular expression /(\w+)\s(\w+)/, what
do the capturing groups (\w+) represent?
29. Question: Which character is used to escape a
metacharacter in a regular expression?
30. Question: What is the purpose of the search() method in
JavaScript when used with a regular expression?
31. Question: Which method splits a string into an array of
substrings based on a regular expression?
32. Question: What is the purpose of the \w character class in
a regular expression?
33. Question: In the regular expression
/(\d{2})\/(\d{2})\/(\d{4})/, what do the capturing groups
(\d{2}), (\d{2}), and (\d{4}) represent?
34. Question: What is the purpose of the \W character class in
a regular expression?
35. Question: In the regular expression /(\b\w+\b)\s\1/,
what does \1 represent?
36. Question: What does the \b anchor do in a regular
expression?
37. Question: Which quantifier matches one or more
occurrences of the preceding element?
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses [Link]
27
38. Question: In the regular expression /^[A-Z][a-z]*$/,
what does the pattern represent?
39. Question: What is the purpose of the \n escape sequence
in a regular expression?
40. Question: In the regular expression
/(\d+)\s(?:years?|yrs?)/, what does (?:years?|yrs?)
represent?
41. Question: What is the purpose of the \s character class in
a regular expression?
42. Question: In the regular expression
/(\d{3})-(\d{2})-(\d{4})/, what does the capturing group
(\d{3}) represent?
43. Question: Which method is used to test if a string
contains a pattern in a regular expression?
44. Question: What does the \S+ pattern represent in a
regular expression?
45. Question: In the regular expression /^[a-zA-Z]\w*$/,
what does the pattern represent?
46. Question: What is the purpose of the \ character in a
regular expression?
47. Question: In the regular expression /[^aeiou]/, what does
the character class [^aeiou] represent?
48. Question: What does the flags property of a RegExp
object return?
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses [Link]
28
49. Question: What is the purpose of the $ anchor in a regular
expression?
50. Question: In the regular expression /([a-z]+)\s(\d+)/,
what do the capturing groups ([a-z]+) and (\d+) represent?
A) RegexObject
B) String
C) RegExp
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses [Link]
29
Question: Which character is used as the delimiter for a
regular expression literal?
A) /
B) |
C) #
A) +
B) *
C) ?
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses [Link]
30
Question: What is the purpose of the ^ anchor in a regular
expression?
A) \d
B) \w
C) \s
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses [Link]
31
Question: What is the purpose of the non-capturing group (?: )
in a regular expression?
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses [Link]
32
Question: What is the purpose of the g flag in a regular
expression?
A) .
B) \n
C) *
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses [Link]
33
Question: Which quantifier matches exactly three occurrences
of the preceding element?
A) {3}
B) {3,}
C) {0,3}
A) exec()
B) test()
C) matchAll()
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses [Link]
34
Question: In the regular expression
/^[\w.-]+@[a-z]+\.[a-z]+$/i, what does the i flag represent?
A) Case-sensitive match
B) Global match
C) Case-insensitive match
A) exec()
B) match()
C) search()
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses [Link]
35
Question: In the regular expression /[^0-9]/, what does the
character class [^0-9] represent?
A) {2,4}
B) {2}
C) {4,}
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses [Link]
36
Question: In the regular expression /^\d{3}-\d{2}-\d{4}$/,
what does the pattern represent?
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses [Link]
37
Question: In the regular expression /(\w+)\s(\w+)/, what do
the capturing groups (\w+) represent?
A) !
B) \
C) |
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses [Link]
38
Question: Which method splits a string into an array of
substrings based on a regular expression?
A) split()
B) slice()
C) splice()
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses [Link]
39
Question: What is the purpose of the \W character class in a
regular expression?
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses [Link]
40
Question: Which quantifier matches one or more occurrences
of the preceding element?
A) ?
B) *
C) +
A) Matches a sentence
B) Matches a capitalized word
C) Matches an email address
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses [Link]
41
Question: In the regular expression /(\d+)\s(?:years?|yrs?)/,
what does (?:years?|yrs?) represent?
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses [Link]
42
Question: Which method is used to test if a string contains a
pattern in a regular expression?
A) contains()
B) includes()
C) test()
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses [Link]
43
Question: What is the purpose of the \ character in a regular
expression?
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses [Link]
44
Question: What is the purpose of the $ anchor in a regular
expression?
Quiz Answers
1. Answer: C) A pattern describing a certain amount of text
2. Answer: C) RegExp
3. Answer: B) Tests a string for a match against a regular expression
4. Answer: A) /
5. Answer: A) An array containing matched text
6. Answer: B) *
7. Answer: B) Matches the start of a string
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses [Link]
45
8. Answer: A) \d
9. Answer: C) Matches one or more occurrences of the character "b"
10. Answer: C) Groups expressions without capturing the matched result
11. Answer: A) Matches zero or one occurrence of the preceding element
12. Answer: B) Negative Lookahead (?! )
13. Answer: A) Global match (find all matches)
14. Answer: A) .
15. Answer: A) Matches any vowel
16. Answer: A) {3}
17. Answer: B) Replaces text in a string with a specified value
18. Answer: C) matchAll()
19. Answer: C) Case-insensitive match
20. Answer: A) Matches a word boundary
21. Answer: B) match()
22. Answer: B) Matches any non-digit
23. Answer: A) {2,4}
24. Answer: A) Matches any digit
25. Answer: A) Social Security Number (SSN) format
26. Answer: B) An array of all matches of a pattern, including capturing groups
27. Answer: B) Matches any non-whitespace character
28. Answer: B) Captures the first and last names
29. Answer: B) \
30. Answer: A) Searches for a pattern in a string and returns the index of the first
match
31. Answer: A) split()
32. Answer: B) Matches any word character
33. Answer: A) Captures the day, month, and year in a date
34. Answer: B) Matches any non-word character
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses [Link]
46
35. Answer: A) Matches the same word as captured in the first capturing group
36. Answer: A) Matches a word boundary
37. Answer: C) +
38. Answer: B) Matches a capitalized word
39. Answer: A) Matches a newline character
40. Answer: B) Creates a non-capturing group for "years" or "yrs"
41. Answer: A) Matches any whitespace character
42. Answer: B) Matches any three digits
43. Answer: C) test()
44. Answer: B) Matches one or more occurrences of any non-whitespace character
45. Answer: B) Matches a variable name in JavaScript
46. Answer: A) Represents an escape character
47. Answer: B) Matches any non-vowel
48. Answer: C) A string containing the flags used in the regular expression
49. Answer: A) Matches the end of a string
50. Answer: A) Captures a word and a number in a string
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses [Link]
47