MCQs Of Day 26: Regular Expressions in JavaScript
1. What
does the \d character class match in a regular expression?
A) Any non-digit character
B) Any digit character
C) Any whitespace character
D) Any alphanumeric character
Answer: B) Any
digit character
Explanation: The \d matches any digit (0-9).
2. What
does the regular expression /\bword\b/ match?
A) The word "word"
surrounded by whitespace
B) Any word containing "word"
C) The word "word" as a whole word
D) Any part of the word "word"
Answer: C) The
word "word" as a whole word
Explanation: The \b ensures that "word" is matched as a
complete word (bounded by word boundaries).
3. What
is the purpose of the ^ symbol in a regular expression?
A) Matches the end of a string
B) Matches any character
C) Anchors the match to the beginning of a string
D) Matches a space character
Answer: C)
Anchors the match to the beginning of a string
Explanation: The ^ symbol matches the start of the string.
4. How
would you match any character except a newline in JavaScript using regular
expressions?
A) /[^]/
B) /./
C) /\w/
D) /[\s]/
Answer: B) /./
Explanation: The dot . matches any character except newlines.
5. Which
method can you use to test if a regular expression matches a string in
JavaScript?
A) RegExp()
B) match()
C) test()
D) exec()
Answer: C) test()
Explanation: The test() method checks if the regular expression matches
the string.
6. What
does the regular expression /\d+/ match?
A) A single digit
B) A sequence of one or more digits
C) A non-digit character
D) Any alphanumeric character
Answer: B) A
sequence of one or more digits
Explanation: The + quantifier matches one or more occurrences of the
preceding character or group.
7. Which
of the following regular expressions matches exactly four digits in a row?
A) /\d{4}/
B) /\d+
C) /\d{4,6}/
D) /\d{4,}/
Answer: A) /\d{4}/
Explanation: The {4} specifies that exactly four digits should be
matched.
8. How
would you match a string that contains the word "apple" regardless of
case?
A) /apple/i
B) /apple/g
C) /apple/
D) /apple/i+
Answer: A) /apple/i
Explanation: The i flag makes the regular expression case-insensitive.
9. What
does the $ symbol do in a regular expression?
A) Matches the start of the
string
B) Matches any digit
C) Matches the end of the string
D) Matches any non-alphanumeric character
Answer: C)
Matches the end of the string
Explanation: The $ symbol anchors the match to the end of the string.
10. Which
of the following regular expressions matches an email address?
A) /^[a-zA-Z0-9]+@[a-zA-Z0-9]+\.[a-zA-Z]{2,6}$/
B) /\w+@\w+\.\w+/
C) /^.+\@.+\..+$/
D) /^\w{5,}@\w{3,}\.\w{2,}$/
Answer: A) /^[a-zA-Z0-9]+@[a-zA-Z0-9]+\.[a-zA-Z]{2,6}$/
Explanation: This regex matches a typical email address format.
11. What
is the result of calling the exec() method in JavaScript?
A) It matches all occurrences of
the pattern
B) It returns an array of matches
C) It replaces the matched string
D) It only checks for the pattern's presence
Answer: B) It
returns an array of matches
Explanation: The exec() method returns an array with the match results
or null if no match is found.
12. What
is the output of /a{2,4}/.test("aa")?
A) false
B) true
C) undefined
D) null
Answer: B) true
Explanation: The regex matches between 2 and 4 occurrences of 'a', and
"aa" contains exactly two 'a's.
13. How
would you use a regular expression to match a space character in JavaScript?
A) /\s/
B) / /
C) /\w/
D) /\d/
Answer: A) /\s/
Explanation: The \s matches any whitespace character (spaces, tabs,
newlines).
14. What
does the \b in a regular expression represent?
A) Any word character
B) A word boundary
C) A backspace character
D) A break character
Answer: B) A
word boundary
Explanation: The \b asserts a position between a word character and a
non-word character.
15. What
does the regular expression /\w+/ match?
A) One or more non-alphanumeric
characters
B) One or more digits
C) One or more word characters (letters, digits, and underscore)
D) One or more whitespace characters
Answer: C) One
or more word characters (letters, digits, and underscore)
Explanation: The \w matches any word character (alphanumeric characters
and underscore).
16. Which
method is used to replace parts of a string with a regular expression match in
JavaScript?
A) replace()
B) replaceAll()
C) split()
D) exec()
Answer: A) replace()
Explanation: The replace() method is used to replace parts of a string
based on a regular expression match.
17. What
does the regular expression /\b\w{5}\b/ match?
A) A word of exactly five
characters
B) A word with five or more characters
C) A word containing five characters
D) Any character repeated five times
Answer: A) A
word of exactly five characters
Explanation: The {5} quantifier matches exactly five occurrences of the
preceding character.
18. What
does the g flag in a regular expression do?
A) Matches the string globally
across multiple lines
B) Matches the first occurrence of the pattern
C) Performs a case-sensitive match
D) Applies a global replace function
Answer: A)
Matches the string globally across multiple lines
Explanation: The g flag stands for "global," meaning it will
find all matches in the string.
19. What
is the correct regular expression to match a phone number in the format 123-456-7890?
A) /\d{3}-\d{3}-\d{4}/
B) /\d{10}/
C) /\(\d{3}\)-\d{3}-\d{4}/
D) /\d{3}-\d{3}-\d{3}/
Answer: A) /\d{3}-\d{3}-\d{4}/
Explanation: This regex matches the phone number format with three
digits, a hyphen, another three digits, another hyphen, and four digits.
20. How
would you match all occurrences of the word "cat" in a string,
ignoring case?
A) /cat/i
B) /cat/g
C) /cat/ig
D) /cat/
Answer: C) /cat/ig
Explanation: The i flag makes it case-insensitive, and the g flag allows
matching all occurrences.
21. Which
of the following regular expressions matches a string starting with the word
"start"?
A) /^start/
B) /start$/
C) /start/g
D) /start/
Answer: A) /^start/
Explanation: The ^ anchor matches the beginning of the string.
22. How
would you match any URL in the format http://example.com using regular
expressions?
A) /http:\/\/[a-zA-Z0-9]+(\.[a-zA-Z]{2,})+/
B) /http:\/\w+\.\w+/
C) /http:\/\/[a-zA-Z0-9]+/
D) /http:\/\/\w+\.\w+/
Answer: A) /http:\/\/[a-zA-Z0-9]+(\.[a-zA-Z]{2,})+/
Explanation: This regex matches a URL starting with http:// followed by
domain names and TLDs.
23. What
does the regular expression /[^a-zA-Z]/ match?
A) Any non-alphabetic character
B) Any lowercase letter
C) Any uppercase letter
D) Any alphanumeric character
Answer: A) Any
non-alphabetic character
Explanation: The [^a-zA-Z] negates the character class and matches any
character that is not an uppercase or lowercase letter.
24. What
does the ? quantifier do in a regular expression?
A) Matches the preceding element
one or more times
B) Matches the preceding element zero or more times
C) Matches the preceding element exactly once
D) Matches the preceding element zero or once
Answer: D)
Matches the preceding element zero or once
Explanation: The ? quantifier matches the preceding character zero or
one time.
25. Which
of the following matches a string containing only digits?
A) /^\d+$/
B) /\d+/
C) /^\d*$/
D) /\d$/
Answer: A) /^\d+$/
Explanation: The ^ and $ anchor the match to the start and end of the
string, respectively, ensuring the string contains only digits.
26. How
do you match a literal dot (.) in a string using regular expressions?
A) \.
B) \\.
C) /\./
D) /dot/
Answer: A) \.
Explanation: To match a literal dot, you need to escape it using a
backslash (\.).
27. Which
of the following is the correct way to match an optional "s" at the
end of the word "cat"?
A) /cat{1}s?/
B) /cats?/
C) /cat{s}/
D) /cat{s?}/
Answer: B) /cats?/
Explanation: The s? indicates that the "s" character is
optional.
28. What
does the | symbol represent in regular expressions?
A) Logical OR
B) Match zero or more occurrences
C) Grouping of elements
D) Anchoring a match
Answer: A)
Logical OR
Explanation: The | is used to match either the expression before or
after it.
29. Which
of the following regular expressions will match a string that does not contain
any digits?
A) /\D+/
B) /\d+/
C) /[^0-9]/
D) /[^a-zA-Z0-9]/
Answer: A) /\D+/
Explanation: The \D matches any non-digit character.
30. What
is the role of the parentheses () in regular expressions?
A) Grouping characters into a
subexpression
B) Matching any character
C) Anchoring a match
D) Escaping a special character
Answer: A)
Grouping characters into a subexpression
Explanation: Parentheses are used to group parts of a regex into
subexpressions.
31. Which
of the following will match a string that contains only vowels?
A) /^[aeiouAEIOU]+$/
B) /[aeiou]/
C) /\b[aeiou]\b/
D) /[^aeiou]/
Answer: A) /^[aeiouAEIOU]+$/
Explanation: This regex matches strings that only contain vowels
(case-insensitive).
32. How
would you match any string that contains the word "error"
(case-insensitive)?
A) /error/i
B) /error/
C) /^error$/i
D) /\berror\b/i
Answer: A) /error/i
Explanation: The i flag makes the match case-insensitive.
4o mini
You said:
more
ChatGPT
said:
ChatGPT
Here are additional regular
expression questions:
33. Which
of the following regular expressions will match a string that starts with
"abc"?
A) /^abc/
B) /abc$/
C) /abc/
D) /abc^/
Answer: A) /^abc/
Explanation: The ^ anchors the match to the beginning of the string,
ensuring it starts with "abc".
34. What
does the + quantifier do in a regular expression?
A) Matches the preceding element
one or more times
B) Matches the preceding element zero or more times
C) Matches the preceding element exactly once
D) Matches the preceding element one or fewer times
Answer: A)
Matches the preceding element one or more times
Explanation: The + quantifier matches the preceding character one or
more times.
35. Which
of the following regular expressions will match a string that contains exactly
three letters?
A) /^[a-zA-Z]{3}$/
B) /^[a-zA-Z]+$/
C) /^[a-zA-Z]{3,}$/
D) /[a-zA-Z]{3}/
Answer: A) /^[a-zA-Z]{3}$/
Explanation: The {3} quantifier ensures exactly three letters are
matched, and the ^ and $ anchors ensure that the whole string contains only
those three letters.
36. Which
of the following matches any whitespace character (spaces, tabs, newlines)?
A) \s
B) \S
C) \d
D) \w
Answer: A) \s
Explanation: The \s matches any whitespace character, including spaces,
tabs, and newlines.
37. How
can you match a word boundary in a regular expression?
A) \b
B) \B
C) \w
D) \d
Answer: A) \b
Explanation: The \b metacharacter matches a word boundary, which is the
position between a word character (like letters or digits) and a non-word
character.
38. Which
of the following regular expressions will match a string that contains an email
address?
A) /\w+@\w+\.\w+/
B) /\w+@(\w+\.)+\w+/
C) /[a-zA-Z0-9]+@[a-zA-Z]+\.[a-zA-Z]+/
D) /\S+@\S+\.\S+/
Answer: D) /\S+@\S+\.\S+/
Explanation: The \S matches any non-whitespace character, and this regex
will match email-like strings that do not contain spaces.
39. Which
of the following expressions will match a string that contains at least one
digit?
A) /\d+/
B) /\d*/
C) /[0-9]/
D) /^d+$/
Answer: A) /\d+/
Explanation: The \d+ matches one or more digits in the string.
40. How
do you match a string that starts with "http" and ends with
".com"?
A) /^http.*\.com$/
B) /http.*\.com/
C) /^http.*com$/
D) /http.*\.com$/
Answer: A) /^http.*\.com$/
Explanation: The ^ anchors the match to the start of the string, and the
.* matches any characters between "http" and ".com".
41. How
do you match exactly two characters "ab" followed by zero or more
"c"s in a string?
A) /ab{2}c*/
B) /ab{2}c+/
C) /ab{2,}c*/
D) /ab{1}c*/
Answer: A) /ab{2}c*/
Explanation: The {2} quantifier means exactly two occurrences of
"b" after "a", and c* matches zero or more occurrences of
"c".
42. Which
of the following will match a string containing any non-word characters?
A) \W
B) \w
C) \D
D) \d
Answer: A) \W
Explanation: The \W matches any character that is not a word character,
i.e., not a letter, digit, or underscore.
43. Which
regular expression will match any string that ends with "ing"?
A) /^.*ing$/
B) /.*ing$/
C) /ing$/
D) /.*ing/
Answer: B) /.*ing$/
Explanation: The .* matches any number of any characters, and $ ensures
the string ends with "ing".
44. What
does the \d metacharacter match in a regular expression?
A) Any alphanumeric character
B) Any digit
C) Any letter
D) Any non-word character
Answer: B) Any
digit
Explanation: The \d matches any single digit, equivalent to [0-9].
45. How
would you match a string that contains only letters and numbers (no special
characters)?
A) /^[a-zA-Z0-9]+$/
B) /^[a-zA-Z0-9]*$/
C) /^[\w]+$/
D) /^[^a-zA-Z0-9]$/
Answer: A) /^[a-zA-Z0-9]+$/
Explanation: This regex ensures that the string contains only letters
(both lowercase and uppercase) and numbers, with the ^ and $ anchoring the
match to the start and end of the string.
46. Which
of the following regular expressions matches a string with only the first
character as a capital letter and the rest in lowercase?
A) /^[A-Z][a-z]*$/
B) /^[a-z][A-Z]*$/
C) /^[A-Z][a-z]+$/
D) /^[a-z][A-Z]+$/
Answer: A) /^[A-Z][a-z]*$/
Explanation: The ^[A-Z] ensures the first character is uppercase, and [a-z]*
ensures the remaining characters are lowercase (zero or more).
47. How
do you match a string that contains at least one occurrence of "foo"
anywhere in it?
A) /.*foo.*/
B) /foo+/
C) /^foo.*$/
D) /foo/
Answer: A) /.*foo.*/
Explanation: The .* before and after "foo" ensures that
"foo" can appear anywhere in the string.
48. Which
regular expression will match any string that contains a date in the format
"DD-MM-YYYY"?
A) /\d{2}-\d{2}-\d{4}/
B) /\d{1,2}-\d{1,2}-\d{4}/
C) /\d{2}/-\d{2}/-\d{4}/
D) /\d{2}-\d{4}-\d{2}/
Answer: A) /\d{2}-\d{2}-\d{4}/
Explanation: This regex matches a string with a two-digit day, two-digit
month, and four-digit year in the format "DD-MM-YYYY".
49. What
does the \B metacharacter match in a regular expression?
A) A non-word boundary
B) A word boundary
C) Any non-alphanumeric character
D) Any word character
Answer: A) A
non-word boundary
Explanation: The \B metacharacter matches any position that is not a
word boundary.
50. Which
of the following regular expressions will match a string with exactly three
digits?
A) /^\d{3}$/
B) /^\d+/
C) /\d{3,}/
D) /\d{3,5}/
Answer: A) /^\d{3}$/
Explanation: The {3} quantifier ensures exactly three digits, and ^ and $
anchor the match to the start and end of the string.
4o mini
You said:
more
ChatGPT
said:
ChatGPT
Here are more questions related
to Regular Expressions in JavaScript:
51. Which
of the following regular expressions will match a string that contains only
uppercase letters?
A) /^[A-Z]+$/
B) /^[a-z]+$/
C) /[A-Z]/
D) /[^A-Z]+/
Answer: A) /^[A-Z]+$/
Explanation: The ^[A-Z]+$ ensures that the string contains only
uppercase letters from start to end.
52. Which
of the following will match a string containing a valid US phone number format,
such as "(123) 456-7890"?
A) /^\(\d{3}\)\s\d{3}-\d{4}$/
B) /^\(\d{2}\)\s\d{2}-\d{3}$/
C) /^\d{3}-\d{3}-\d{4}$/
D) /^\(\d{3}\)-\d{3}-\d{4}$/
Answer: A) /^\(\d{3}\)\s\d{3}-\d{4}$/
Explanation: This regex matches a phone number in the format (XXX)
XXX-XXXX, where \(\d{3}\) matches the area code in parentheses, \s matches the
space, and \d{3}-\d{4} matches the rest of the number.
53. Which
of the following regular expressions will match a string that only contains
digits and commas (e.g., "1,234,567")?
A) /^\d+(,\d{3})*$/
B) /^\d{3},\d{3}+$/
C) /^\d{1,3}(,\d{3}){1,2}$/
D) /^\d+$/
Answer: A) /^\d+(,\d{3})*$/
Explanation: The regex \d+(,\d{3})* ensures that the string contains one
or more digits, followed by zero or more groups of commas followed by exactly
three digits.
54. Which
of the following metacharacters is used to match any character except a newline
in JavaScript regular expressions?
A) .
B) \n
C) \r
D) \S
Answer: A) .
Explanation: The dot . matches any character except for newline characters.
55. What
does the regular expression /\d{2,4}/ match?
A) A sequence of two or more
digits
B) A sequence of exactly two digits
C) A sequence of two to four digits
D) A sequence of four digits or more
Answer: C) A
sequence of two to four digits
Explanation: The {2,4} quantifier ensures the match will consist of
between 2 and 4 digits.
56. Which
of the following will match any string that does not contain a digit?
A) /\D+/
B) /\d+/
C) /^\D*$/
D) /[^0-9]+/
Answer: C) /^\D*$/
Explanation: The \D matches non-digit characters, and * allows for zero
or more non-digit characters. The ^ and $ anchors ensure that the entire string
does not contain digits.
57. Which
of the following regular expressions will match a string that contains a date
in the format "MM/DD/YYYY"?
A) /\d{2}\/\d{2}\/\d{4}/
B) /\d{2}-\d{2}-\d{4}/
C) /\d{4}\/\d{2}\/\d{2}/
D) /\d{1,2}\/\d{1,2}\/\d{4}/
Answer: A) /\d{2}\/\d{2}\/\d{4}/
Explanation: This regex matches a string with two digits for the month,
two digits for the day, and four digits for the year, separated by slashes.
58. Which
of the following regular expressions will match an email address?
A) /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/
B) /^[a-zA-Z0-9]+@[a-zA-Z0-9]+\.[a-zA-Z]{2,}$/
C) /^[a-zA-Z0-9]{2,}@[a-zA-Z0-9]{2,}\.[a-zA-Z]{2,}$/
D) /\w+@\w+\.\w+/
Answer: A) /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/
Explanation: This regex matches most common email formats, where [a-zA-Z0-9._-]+
matches the username part, @ separates the username and domain, and [a-zA-Z0-9.-]+\.[a-zA-Z]{2,}
matches the domain and extension.
59. Which
of the following regular expressions will match a string that contains a word
boundary before "foo"?
A) /\bfoo/
B) /foo\b/
C) /\Bfoo/
D) /foo\B/
Answer: A) /\bfoo/
Explanation: The \b matches a word boundary before the "foo",
ensuring "foo" is at the start of a word.
60. Which
of the following will match a string that contains exactly one uppercase letter
followed by one or more lowercase letters?
A) /^[A-Z][a-z]+$/
B) /^[A-Z][a-z]*$/
C) /^[A-Za-z]+$/
D) /^[a-zA-Z]{2,}$/
Answer: A) /^[A-Z][a-z]+$/
Explanation: The ^[A-Z] ensures the string starts with an uppercase
letter, and [a-z]+ ensures that it is followed by one or more lowercase
letters.
61. How
would you match a string that contains the word "JavaScript" exactly,
regardless of case?
A) /\bjavascript\b/i
B) /\bJavaScript\b/i
C) /\bjavascript\b/
D) /JavaScript/i
Answer: A) /\bjavascript\b/i
Explanation: The \b ensures a word boundary, and the i flag makes the
regex case-insensitive, matching any case of the word "JavaScript".
62. What
does the regular expression /^.{5}$/ match?
A) A string of exactly 5
characters
B) A string of at least 5 characters
C) A string that is exactly 5 digits
D) A string that contains exactly 5 words
Answer: A) A
string of exactly 5 characters
Explanation: The . matches any character except a newline, and {5}
ensures exactly five characters are matched. The ^ and $ anchor the match to
the start and end of the string.
63. Which
of the following regular expressions will match a string that contains a valid
IP address?
A) /^\d{1,3}(\.\d{1,3}){3}$/
B) /\d{1,3}(\.\d{1,3}){4}/
C) /^\d{3}(\.\d{2}){3}$/
D) /^(\d{1,3}){4}$/
Answer: A) /^\d{1,3}(\.\d{1,3}){3}$/
Explanation: This regex matches a valid IP address format, where \d{1,3}
matches a segment of the IP, and (\.\d{1,3}){3} matches the three remaining
segments, each prefixed by a dot.
64. What
does the * quantifier do in a regular expression?
A) Matches zero or more
occurrences of the preceding element
B) Matches one or more occurrences of the preceding element
C) Matches exactly one occurrence of the preceding element
D) Matches zero occurrences of the preceding element
Answer: A)
Matches zero or more occurrences of the preceding element
Explanation: The * quantifier allows the preceding element to be matched
zero or more times.
65. Which
of the following regular expressions matches strings that do not start with the
word "admin"?
A) /^(?!admin)/
B) /^(?!admin).*$/
C) /^(admin)?/
D) /admin/
Answer: B) /^(?!admin).*$/
Explanation: The (?!admin) is a negative lookahead that ensures the
string does not start with "admin".
Let me know if you would like
more questions!
4o mini
You said:
more
ChatGPT
said:
ChatGPT
Here are more multiple-choice
questions related to Regular Expressions in JavaScript:
66. Which
of the following regular expressions will match strings that contain only
letters (both lowercase and uppercase)?
A) /^[a-z]+$/
B) /^[a-zA-Z]+$/
C) /^[A-Za-z]+$/
D) /^[A-Z]+$/
Answer: B) /^[a-zA-Z]+$/
Explanation: The [a-zA-Z] matches both lowercase and uppercase letters,
and + ensures that one or more letters are matched.
67. What
does the regular expression /\bword\b/ match?
A) The word "word"
surrounded by non-word characters
B) The word "word" anywhere in the string
C) The word "word" at the beginning of a string
D) The word "word" at the end of a string
Answer: A) The
word "word" surrounded by non-word characters
Explanation: The \b is a word boundary anchor, ensuring that
"word" is a whole word, and not part of another word.
68. What
does the regular expression /[^a-z]/ match?
A) Any string containing
lowercase letters
B) Any character that is not a lowercase letter
C) Any string that contains a digit
D) Any string that contains a special character
Answer: B) Any
character that is not a lowercase letter
Explanation: The [^a-z] matches any character that is not in the range
from a to z (i.e., not a lowercase letter).
69. How
would you match any string that starts with "test" and ends with
"123"?
A) /^test123$/
B) /^test.*123$/
C) /^test123/
D) /test123$/
Answer: B) /^test.*123$/
Explanation: The ^test ensures the string starts with "test", .*
allows any characters in between, and 123$ ensures the string ends with
"123".
70. Which
of the following will match a string that contains only numeric digits,
including optional commas?
A) /^\d+$/
B) /^\d+(,\d{3})*$/
C) /^\d{1,3}(\,\d{3})*$/
D) /^\d{3}(-\d{3})*$/
Answer: B) /^\d+(,\d{3})*$/
Explanation: This regular expression matches a number with optional
comma separators in thousands (e.g., 1,000, 1,000,000).
71. Which
of the following will match any string that contains at least one digit?
A) /\d+/
B) /^[0-9]$/
C) /[0-9]{1,}/
D) /\d*$/
Answer: A) /\d+/
Explanation: The \d+ matches one or more digits in a string.
72. How
would you match a string that contains one or more uppercase letters, followed
by exactly three digits?
A) /^[A-Z]{1,}[0-9]{3}$/
B) /^[A-Z][0-9]{3}$/
C) /^[A-Z]{3}[0-9]{1,}$/
D) /^[A-Z]+[0-9]+$/
Answer: B) /^[A-Z][0-9]{3}$/
Explanation: This regex matches exactly one uppercase letter, followed
by exactly three digits.
73. What
will the regular expression /\d{3,5}/ match?
A) Exactly 3 digits
B) A string with 3 to 5 digits
C) A string with at least 5 digits
D) 5 or more digits, but not less than 3
Answer: B) A
string with 3 to 5 digits
Explanation: The {3,5} quantifier allows for matching between 3 and 5
digits.
74. Which
of the following matches any string that contains only uppercase letters and
numbers?
A) /^[A-Z0-9]+$/
B) /^[a-z0-9]+$/
C) /^[A-Za-z0-9]+$/
D) /[A-Z0-9]+/
Answer: A) /^[A-Z0-9]+$/
Explanation: The [A-Z0-9] matches uppercase letters and digits, and the +
ensures one or more of these characters.
75. Which
of the following regular expressions will match a string that does not contain
the word "hello"?
A) /^(?!.*hello).*$/
B) /^(?!hello).*$/
C) /hello/
D) /^(?!hello).*$/
Answer: A) /^(?!.*hello).*$/
Explanation: The (?!.*hello) is a negative lookahead that ensures
"hello" does not appear anywhere in the string.
76. How
would you match a string that contains exactly three words separated by spaces?
A) /^\w+\s\w+\s\w+$/
B) /^\w+\s\w+\s\w{1,}$/
C) /^\w+ \w+ \w+$/
D) /^\w{3} \w{3} \w{3}$/
Answer: A) /^\w+\s\w+\s\w+$/
Explanation: The \w+ matches one or more word characters, \s matches a
space, and the ^ and $ anchors ensure the string contains exactly three words.
77. What
does the + quantifier do in a regular expression?
A) Matches one or more
occurrences of the preceding element
B) Matches exactly one occurrence of the preceding element
C) Matches zero or more occurrences of the preceding element
D) Matches at least one occurrence of the preceding element
Answer: A)
Matches one or more occurrences of the preceding element
Explanation: The + quantifier matches one or more occurrences of the
preceding element.
78. Which
regular expression will match a valid hexadecimal color code, such as #ff5733?
A) /^#[0-9a-fA-F]{6}$/
B) /^[0-9a-fA-F]{6}$/
C) /^#([0-9a-fA-F]{6}|[0-9a-fA-F]{3})$/
D) /^#[a-fA-F0-9]{6}$/
Answer: A) /^#[0-9a-fA-F]{6}$/
Explanation: This regex matches a string starting with # followed by
exactly 6 characters, each of which can be a digit or a letter from a to f (for
a hexadecimal color).
79. How
would you match a string that does not contain any digits?
A) /\D+/
B) /[^0-9]+/
C) /\d+/
D) /\w+/
Answer: B) /[^0-9]+/
Explanation: The [^0-9]+ matches one or more characters that are not
digits.
80. Which
of the following regular expressions will match a valid URL that starts with
"http" or "https"?
A) /^https?:\/\/[a-zA-Z0-9.-]+\.[a-zA-Z]{2,6}$/
B) /^https:\/\/[a-zA-Z0-9.-]+\.[a-zA-Z]{2,6}$/
C) /http[s]?:\/\/[a-zA-Z0-9.-]+\.[a-zA-Z]{2,6}/
D) /^https?:\/\/[a-zA-Z0-9]+.[a-zA-Z]{2,}$/
Answer: A) /^https?:\/\/[a-zA-Z0-9.-]+\.[a-zA-Z]{2,6}$/
Explanation: The https? matches "http" or "https",
and \/\/[a-zA-Z0-9.-]+\.[a-zA-Z]{2,6} matches a domain name and top-level
domain.
Let me know if you need more!
4o mini
You said:
more
ChatGPT
said:
ChatGPT
Here are additional
multiple-choice questions (MCQs) related to Regular Expressions in
JavaScript:
81. What
does the regular expression /\d{3}-\d{2}-\d{4}/ match?
A) A U.S. Social Security Number
(SSN)
B) A date in the format DD-MM-YYYY
C) A phone number with area code
D) A hexadecimal color code
Answer: A) A
U.S. Social Security Number (SSN)
Explanation: The regular expression matches a U.S. Social Security
Number (SSN) in the format XXX-XX-XXXX, where X is a digit.
82. Which
of the following regular expressions matches a string that contains only
uppercase letters, lowercase letters, and numbers?
A) /^[a-zA-Z0-9]+$/
B) /^[A-Za-z0-9]+$/
C) /^[A-Z0-9]+$/
D) /^[a-z]+$/
Answer: B) /^[A-Za-z0-9]+$/
Explanation: This regex ensures that only uppercase letters, lowercase
letters, and numbers are allowed in the string.
83. Which
of the following regular expressions will match any string that contains at
least one lowercase letter?
A) /[a-z]+/
B) /^[a-z]+$/
C) /[A-Za-z]/
D) /[a-z]$/
Answer: A) /[a-z]+/
Explanation: The [a-z]+ matches one or more lowercase letters anywhere
in the string.
84. What
does the \w metacharacter match in a regular expression?
A) A word boundary
B) A single alphanumeric character (letters, digits, and underscore)
C) A whitespace character
D) A non-word character
Answer: B) A
single alphanumeric character (letters, digits, and underscore)
Explanation: The \w metacharacter matches any word character, which
includes letters, digits, and underscores.
85. What
will the regular expression /^\d{4}-\d{2}-\d{2}$/ match?
A) A date in the format YYYY-MM-DD
B) A phone number with a hyphen separator
C) A four-digit number followed by a two-digit month
D) A hexadecimal color code
Answer: A) A
date in the format YYYY-MM-DD
Explanation: The regex matches a date in the format YYYY-MM-DD, where \d{4}
matches the year, \d{2} matches the month, and another \d{2} matches the day.
86. Which
of the following expressions is used to match a string that contains either
"apple" or "orange"?
A) /apple|orange/
B) /apple,orange/
C) /^apple|orange$/
D) /appleorange/
Answer: A) /apple|orange/
Explanation: The pipe | is used to specify alternatives. This regular
expression matches either "apple" or "orange".
87. What
does the regular expression /\b\w+\b/ match?
A) One word that contains only
letters
B) A sequence of one or more word characters (letters, digits, or underscores)
surrounded by word boundaries
C) A sequence of digits
D) A word boundary
Answer: B) A
sequence of one or more word characters (letters, digits, or underscores)
surrounded by word boundaries
Explanation: The \b represents a word boundary, and \w+ matches one or
more word characters.
88. Which
of the following regular expressions will match a string that contains a valid
email address?
A) /^[a-zA-Z0-9]+@[a-zA-Z0-9]+\.[a-zA-Z]{2,}$/
B) /^[a-zA-Z0-9_]+@[a-zA-Z0-9]+\.[a-zA-Z]{2,4}$/
C) /^[A-Za-z0-9]+@[A-Za-z]+\.[A-Za-z]+$/
D) /^[A-Za-z0-9]+@[A-Za-z]+\.[A-Za-z]{2,4}$/
Answer: A) /^[a-zA-Z0-9]+@[a-zA-Z0-9]+\.[a-zA-Z]{2,}$/
Explanation: This regular expression matches a simple email address
pattern, where the local part consists of alphanumeric characters, followed by @,
and the domain consists of alphanumeric characters and a dot-separated
top-level domain of at least two characters.
89. Which
regular expression will match any string that starts with a capital letter and
ends with a period?
A) /^[A-Z].*\.$/
B) /^[A-Z].*\?/
C) /^[A-Z].*$/
D) /^[A-Z].+$/
Answer: A) /^[A-Z].*\.$/
Explanation: The ^[A-Z] ensures the string starts with an uppercase
letter, .* allows for any number of characters in between, and \.$ ensures the
string ends with a period.
90. How
would you write a regular expression that matches any string with exactly three
digits, with an optional comma separating every group of three digits (e.g.,
"1,000" or "1000000")?
A) /^\d{1,3}(,\d{3})*$/
B) /^\d+(,\d{3})+$/
C) /^\d{3,}(,\d{3})+$/
D) /^\d{3},\d{3}$/
Answer: A) /^\d{1,3}(,\d{3})*$/
Explanation: The regex matches a string that starts with one to three
digits, and optionally, groups of three digits separated by commas.
91. Which
of the following regular expressions will match a string that contains at least
one uppercase letter?
A) /[A-Z]+/
B) /^[A-Z]$/
C) /\b[A-Z]\b/
D) /\d{1,}/
Answer: A) /[A-Z]+/
Explanation: The [A-Z]+ will match one or more uppercase letters
anywhere in the string.
92. What
does the regular expression /^\d{2,4}$/ match?
A) A two to four-digit number
B) A single digit between 2 and 4
C) A number that starts with "2"
D) A string with 2 to 4 characters
Answer: A) A two
to four-digit number
Explanation: The \d{2,4} matches any number between 2 and 4 digits long.
93. Which
regular expression will match a string that starts with a digit, followed by a
hyphen, and then any alphanumeric characters?
A) /^\d-\w+$/
B) /^\d-\w*$/
C) /^\d+\-\w+$/
D) /^\d-\w$/
Answer: A) /^\d-\w+$/
Explanation: This regular expression matches a string that starts with a
digit \d, followed by a hyphen -, and then one or more word characters \w+.
94. Which
of the following regular expressions will match a valid date in the format MM/DD/YYYY?
A) /^\d{2}\/\d{2}\/\d{4}$/
B) /^\d{2}-\d{2}-\d{4}$/
C) /^\d{4}-\d{2}-\d{2}$/
D) /^\d{2}\/\d{4}$/
Answer: A) /^\d{2}\/\d{2}\/\d{4}$/
Explanation: This regular expression matches the date format MM/DD/YYYY,
where each part is two digits for month and day, and four digits for the year,
separated by slashes.
95. What
does the regular expression /(\d{2})/ do?
A) Matches two digits and groups
them together
B) Matches a string that contains exactly two digits
C) Matches exactly two digits, but they must be separated
D) Matches one digit followed by any other character
Answer: A)
Matches two digits and groups them together
Explanation: The parentheses () are used for grouping, so (\d{2})
captures two consecutive digits as a group.
96. How
would you match a string that contains exactly two uppercase letters, followed
by exactly three digits?
A) /^[A-Z]{2}[0-9]{3}$/
B) /^[A-Z]{3}[0-9]{2}$/
C) /^[A-Z0-9]{5}$/
D) /^[A-Za-z]{2}\d{3}$/
Answer: A) /^[A-Z]{2}[0-9]{3}$/
Explanation: This regex matches exactly two uppercase letters followed
by exactly three digits.
97. Which
of the following regular expressions matches only a string with digits and
commas (e.g., "1,000,000")?
A) /^\d+(\,\d{3})*$/
B) /^\d+,\d{3}$/
C) /^\d{1,3}(\,\d{3})+$/
D) /^\d+\,\d+$/
Answer: A) /^\d+(\,\d{3})*$/
Explanation: This regular expression matches numbers that may include
commas for grouping digits in threes.
98. Which
of the following regular expressions will match a string with a time format HH:MM
(hours
) in
24-hour format?
A) /^\d{2}:\d{2}$/
B) /^\d{1,2}:\d{1,2}$/
C) /^\d{2}:\d{1,2}$/
D) /^\d{2}-\d{2}$/
Answer: A) /^\d{2}:\d{2}$/
Explanation: The regex matches two digits for the hours followed by a
colon and two digits for the minutes, ensuring a valid time format.
99. Which
of the following regular expressions will match a string that contains exactly
five digits?
A) /^\d{5}$/
B) /^\d{5,}$/
C) /^\d{5,10}$/
D) /^\d{1,5}$/
Answer: A) /^\d{5}$/
Explanation: This regex matches a string with exactly five digits.
100.
Which regular expression will match a string containing a word that starts with
the letter "a" and ends with the letter "e"?
A) /\ba\w*e\b/
B) /\b\w*a\w*e\b/
C) /^a\w*e$/
D) /a\w*e/
Answer: A) /\ba\w*e\b/
Explanation: This regular expression uses word boundaries (\b) to match
a word that starts with "a" and ends with "e", allowing for
any characters in between.
