LearnFrenzy
Sign in Take a free test

String Methods · Find the Error · Question 7

Error: a RegExp built from raw user input Answer, explanation & discussion

  • Find the error
  • Medium
  • 0 comments
  • Answer explained
  • Moderated community
Question 7 Back to Exercise

The code below stops with an uncaught error. Which statement correctly describes the problem?

main.js
const userQuery = "price (incl";            // typed into a search box
const re = new RegExp(userQuery, "i");
console.log(re.test("Price (incl. GST)"));
  • A It prints true
  • B SyntaxError at run time: "(" opens a group that is never closed – escape user input before new RegExp Correct
  • C It prints false
  • D TypeError
Correct Answer: Option B

new RegExp parses its string at run time and throws "Invalid regular expression: Unterminated group". Escape metacharacters, or use includes on lower-cased text for plain searches.

Discussion & Comments 0

Share your method, a shortcut, or ask about a step you didn't follow.

No comments yet. Be the first to share your solution!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links