Find Numbers with Regular Expressions

Find Numbers with Regular Expressions
0.0 0

#1

okay, so the challenge was/is buggy -

it doesn’t matter if you use -

var expression = /\d+/g; // <--- with "+"

or

var expression = /\d/g; // <--- without "+"

my output is the same - “2”. So what gives? I messed around and changed the testString adding more numbers and I get however many numbers I’ve added, in BOTH cases (with/out plus sign).


#2

I agree that the test string doesn’t show why one would use +. However the challenge text does explain the difference. Namely, using a + will match one or more digits instead of only one.

You can see this effect if you change the testString to something like: "There are 3 cats but 4234 dogs." Using /\d+/g will still find 2 matches (3 and 4234), but /\d/g finds 5 matches (3, 4, 2, 3 and 4).


#3

@EgoDominusVos 2 is the count of digits that are found in the string.

The + allows you to match numbers that are more than 1 digit long like 10 or 999

Try changing the line "There are 3 cats but 4 dogs." to "There are 32 cats but 459 dogs. " and changing the regex to \d and leave out the + to see what the result is.


#4

thanks @BenGitter and @the-thief for expanding on the challenge details.


#5

Thank you for this. I’m starting to get a little frustrated with the whole show you a concept but only explain it in the vaguest way possible. I understand wanting us to look up information for ourselves when we are doing the challenges, but I believe the waypoints need more in terms of why something works, not just that it does.