freeCodeCamp Challenge Guide: Match All Numbers

Match All Numbers


Hints

Hint 1

  • A global flag will help you get through this challenge.

Hint 2

  • Try using a shorthand character for digits.

Solutions

Solution 1 (Click to Show/Hide)
let numRegex = /\d/g;

Code Explanation

  • The \d shorthand character is a shortcut for [0-9], it search for any number between 0 and 9.

My initial code for this was:

// Setup
var testString = "There are 3 cats but 4 dogs.";

// Only change code below this line.

var expression = /\d+4/g;  // Change this line
var expressionToNumbers = testString.match(expression).length;
// Only change code above this line

// This code counts the matches of expression in testString
var digitCount = testString.match(expression).length;

But I got issues with line #6. I just fixed it up now and came up with this:


1 Like