Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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

karan karan
seal-mask
.a{fill-rule:evenodd;}techdegree
karan karan
Web Development Techdegree Student 12,699 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.