Regular Expressions

This section covers the questions for Regular Expressions to clear all your concepts.

Last Updated :
Discuss
Comments

Question 1

What does the g flag in a regular expression do?

  • Makes the search case-insensitive

  • Enables the search to stop after the first match

  • Searches for all matches globally

  • Allows the use of groups in the regular expression

Question 2

Which method tests if a string matches a pattern and returns true or false?

  • exec()

  • test()

  • match()

  • search()

Question 3

What will the following code output?

JavaScript
const regex = /\d+/g;
console.log("abc123xyz456".match(regex));


  • ["123", "456"]

  • [123, 456]

  • ["abc", "xyz"]

  • Error

Question 4

How do you validate an email address using regular expressions?

  • Use the pattern /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/

  • Use the pattern /^\w+@\w+\.\w+$/

  • Use the pattern /[a-z]+/

  • Use the pattern /\d+/

Question 5

What is the purpose of the exec() method in RegExp?

  • Searches a string for a pattern and returns its index

  • Returns a boolean indicating if a match is found

  • Splits a string into an array based on a pattern

  • Returns an array of match details or null if no match is found

Question 6

What will the following code log?

JavaScript
const regex = /a+b*/;
console.log(regex.test("aaabb"));


  • true

  • false

  • null

  • Error

Question 7

Which method can replace all occurrences of a pattern in a string?

  • replaceAll()

  • replace()

  • match()

  • exec()

Question 8

What is the purpose of the search() method in strings?

  • Returns an array of matches

  • Finds the index of the first match

  • Validates a regular expression pattern

  • Replaces the first match with a new string

Question 9

What does the i flag in a regular expression do?

  • Makes the search case-insensitive

  • Searches for all matches globally

  • Stops the search after the first match

  • Allows the use of groups in the regular expression

Question 10

Which method is used to check if a string matches a regular expression pattern?

  • exec()

  • match()

  • search()

  • test()

There are 10 questions to complete.

Take a part in the ongoing discussion