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

Victor Stanciu
Victor Stanciu
Full Stack JavaScript Techdegree Student 11,184 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!

1 Answer

Jason Larson
Jason Larson
7,955 Points

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.