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 Regular Expressions Groups

Victor Stanciu
seal-mask
.a{fill-rule:evenodd;}techdegree
Victor Stanciu
Full Stack JavaScript Techdegree Student 11,196 Points

My solution

This is my solution for the practice:

  1. Match "img_0" followed by a number, then a dot, then either "jpg", "png" or "gif": img_0\d\.(jpg|png|gif)

  2. Match "pro" followed by either "jec", "trac" or "c" then followed by "tor": pro(jec|trac|c)tor

  3. Match "img_" followed by either "sm" or "0<and a number>", then followed by either "_0<and a number>" or nothing, then followed by a dot, then followed by either "jpg" or "png": img_(sm|0\d)(_0\d)?\.(jpg|png)

  4. Match either "www" or "api" followed by a dot, or nothing at all, then followed by either "github" or "teamtreehouse", then followed by a dot then followed by "com": ((www|api)\.)?(github|teamtreehouse)\.com

Hope this helps!

Darrel Valdiviezo
seal-mask
.a{fill-rule:evenodd;}techdegree
Darrel Valdiviezo
Full Stack JavaScript Techdegree Student 13,376 Points

Hey Victor,

Thanks for sharing!

here are my soultions:

1st answer: img_0[\d.]\.(jpg|png|gif)

1st answer alternative: [\w]*\.(jpg|png|gif)

2nd answer: pro(j|t|c)[\w]+

3rd answer: [\w]+\.(jpg|png)

4th answer: (www\.|api\.)?[\w]+\.com ...Victor Stanciu your double group to capture the "." was a nice touch - i duplicated mine :(

1 Answer

I know this lesson was on grouping, but it seems like overkill for numbers 3 and 4. For number 3, all the ones to include had "img_" and something, and the one to exclude didn't have an underscore, so img_.+ works for the given test cases. Again, with number 4, all the grouping seems like overkill, as the ones to be excluded end in ".net" and the ones to match end in ".com", so .+\.com$ gets all but the ones ending in ".net". It would be nice if they gave more explicit details on what to match and exclude, and/or gave broader test sets.