LearnFrenzy
Sign in Take a free test

String Methods · Predict the Output · Question 8

match with and without g Answer, explanation & discussion

  • Output
  • Medium
  • 0 comments
  • Answer explained
  • Moderated community
Question 8 Back to Exercise

What does the following JavaScript code print (console output, as in Node.js)?

main.js
console.log("a1b22c333".match(/\d+/g), "a1b22c333".match(/\d+/)[0], "a1b22c333".match(/\d+/).index);
  • A [ '1', '22', '333' ] 1 1 Correct
  • B [ '1', '22', '333' ] 333 6
  • C [ 1, 22, 333 ] 1 1
  • D [ '1' ] 1 0
Correct Answer: Option A

With g, match lists every match as a string. Without g, it returns the first match ("1") with details such as its index (1).

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