JavaScript Regular Expressions MCQ

1. What is a regular expression in JavaScript?

a) A method for error checking
b) A sequence of characters forming a search pattern
c) A way to write loops
d) A syntax for creating arrays

Answer:

b) A sequence of characters forming a search pattern

Explanation:

In JavaScript, a regular expression is a sequence of characters that forms a search pattern, which can be used for matching and manipulating strings.

2. How do you create a regular expression in JavaScript?

a) Using the RegExp() constructor
b) With the regex keyword
c) Using square brackets []
d) Both a and c

Answer:

a) Using the RegExp() constructor

Explanation:

Regular expressions in JavaScript can be created using the RegExp() constructor or by writing the pattern between forward slashes.

3. Which character in a regular expression means "match any character"?

a) *
b) .
c) ^
d) $

Answer:

b) .

Explanation:

In regular expressions, the dot (.) character is used to match any single character except newline characters.

4. What does the '+' symbol represent in regular expressions?

a) Match the preceding character one or more times
b) Match the preceding character zero or more times
c) Match the preceding character zero or one time
d) Match the beginning of a string

Answer:

a) Match the preceding character one or more times

Explanation:

The '+' symbol in a regular expression indicates that the preceding character should be matched one or more times.

5. How do you indicate that a pattern should match the beginning of a string in a regular expression?

a) Using the ^ symbol
b) Using the $ symbol
c) Using the * symbol
d) Using the + symbol

Answer:

a) Using the ^ symbol

Explanation:

The '^' symbol is used at the beginning of a regular expression to indicate that the match must start at the beginning of the string.

6. What does the '?' symbol mean in regular expressions?

a) Match the preceding character zero or one time
b) Match the preceding character one or more times
c) Match any character
d) Match the end of a string

Answer:

a) Match the preceding character zero or one time

Explanation:

The '?' symbol in a regular expression means that the preceding character is optional and should be matched zero or one time.

7. How do you specify a range of characters in a regular expression?

a) Using parentheses ()
b) Using curly braces {}
c) Using square brackets []
d) Using the pipe | symbol

Answer:

c) Using square brackets []

Explanation:

Square brackets [] are used in regular expressions to specify a range or a set of characters to match.

8. What is the function of the pipe '|' symbol in a regular expression?

a) Indicates the start of a range
b) Serves as a logical OR operator
c) Escapes special characters
d) Groups expressions together

Answer:

b) Serves as a logical OR operator

Explanation:

The pipe '|' symbol in a regular expression acts as a logical OR operator, allowing for the matching of one pattern or another.

9. How do you match a specific number of occurrences of a character in a regular expression?

a) Using the * symbol
b) Using curly braces {}
c) Using parentheses ()
d) Using the + symbol

Answer:

b) Using curly braces {}

Explanation:

Curly braces {} in a regular expression are used to specify the exact number of times a character or a group of characters should be matched.

10. What does the '\d' symbol represent in a regular expression?

a) Any digit character
b) Any non-digit character
c) Any whitespace character
d) Any word character

Answer:

a) Any digit character

Explanation:

The '\d' symbol in a regular expression is used to match any digit character, equivalent to [0-9].

11. How do you make a regular expression case insensitive in JavaScript?

a) Using the 'i' flag
b) Using the 'c' flag
c) Using the 's' flag
d) By adding 'ignoreCase' after the expression

Answer:

a) Using the 'i' flag

Explanation:

Adding the 'i' flag to a regular expression in JavaScript makes the pattern matching case insensitive.

12. What does the '\s' symbol represent in a regular expression?

a) Any whitespace character
b) Any non-whitespace character
c) Any word character
d) Any non-word character

Answer:

a) Any whitespace character

Explanation:

The '\s' symbol in a regular expression is used to match any whitespace character, including spaces, tabs, and line breaks.

13. How do you specify that a pattern should match the end of a string in a regular expression?

a) Using the ^ symbol
b) Using the $ symbol
c) Using the * symbol
d) Using the + symbol

Answer:

b) Using the $ symbol

Explanation:

The '$' symbol is used at the end of a regular expression to indicate that the match must occur at the end of the string.

14. What does the '\w' symbol represent in a regular expression?

a) Any word character
b) Any non-word character
c) Any whitespace character
d) Any digit character

Answer:

a) Any word character

Explanation:

The '\w' symbol in a regular expression matches any word character, which includes letters, digits, and underscores.

15. How do you group parts of a regular expression?

a) Using square brackets []
b) Using curly braces {}
c) Using parentheses ()
d) Using the pipe | symbol

Answer:

c) Using parentheses ()

Explanation:

Parentheses () are used in regular expressions to group parts of the expression, which is useful for applying quantifiers to a group or for capturing groups in a match.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top