Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript Regular Expressions in JavaScript Reformatting User Input Using Replace with Captured Groups

karan Badhwar
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
karan Badhwar
Web Development Techdegree Graduate 18,135 Points

How /(\d*)(\d{2})/ this works

For example the number is 5347 but the first part (\d*) can capture all the 4 digits, so can it just skip the Second part (\d{2}) or the whole expressions evaluates which part to distribute to which section?

1 Answer

Brian Jensen
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree seal-36
Brian Jensen
Treehouse Staff

Great question karan karan!

The two sets of parenthesis () are creating 2 capture groups. That way you can access the first 2 digits and the last 2 digits of that 4 digit string independently, with $1 and $2.