Regular Expressions

Last Updated :
Discuss
Comments

Question 1

What does the g flag in a regular expression do?

  • A

    Makes the search case-insensitive

  • B

    Enables the search to stop after the first match

  • C

    Searches for all matches globally

  • D

    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?

  • A

    exec()

  • B

    test()

  • C

    match()

  • D

    search()

Question 3

What will the following code output?

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


  • A

    ["123", "456"]

  • B

    [123, 456]

  • C

    ["abc", "xyz"]

  • D

    Error

Question 4

How do you validate an email address using regular expressions?

  • A

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

  • B

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

  • C

    Use the pattern /[a-z]+/

  • D

    Use the pattern /\d+/

Question 5

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

  • A

    Searches a string for a pattern and returns its index

  • B

    Returns a boolean indicating if a match is found

  • C

    Splits a string into an array based on a pattern

  • D

    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"));


  • A

    true

  • B

    false

  • C

    null

  • D

    Error

Question 7

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

  • A

    replaceAll()

  • B

    replace()

  • C

    match()

  • D

    exec()

Question 8

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

  • A

    Returns an array of matches

  • B

    Finds the index of the first match

  • C

    Validates a regular expression pattern

  • D

    Replaces the first match with a new string

Question 9

What does the i flag in a regular expression do?

  • A

    Makes the search case-insensitive

  • B

    Searches for all matches globally

  • C

    Stops the search after the first match

  • D

    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?

  • A

    exec()

  • B

    match()

  • C

    search()

  • D

    test()

There are 10 questions to complete.

Take a part in the ongoing discussion