Need help on Find Numbers with Regular Expressions

Use the \d selector to select the number of numbers in the string, allowing for the possibility of one or more digit.

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

// Only change code below this line.

var expression = /.+/g; // Change this line

// Only change code above this line

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

I am not sure what to put in var expression = /.+/g; // Change this line.

Hello @crodier1 here is a nice little Interactive tutorial on Regular Expression that I found helpful.


Change var expression = /.+/g; to var expression = /\d+/g;

The . selects any character, the \d selects only digits and the + selects one or more of the elements matching the previous selector.

I suggest you take some time to learn basic Regex. Try RegexOne.

1 Like

AWESOME! freeCodeCamp and CodeAcademy cover a lot of material in a very short amount of time. It’s great but it is also really nice having alternatives, like the link to RegexOne, to branch off into whatever subject may be covered in a single lesson. It’s all too easy to feel like I’m making progress by slamming through a bunch of lessons, but in reality I’m left with a shallow understanding of the concept and the versatility within. If you have any other resources/links like RegexOne I would love to know! Thank you!