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 Beginning and Ending of Strings

Regex exercise(Group)

I am trying the second exercise, I have this ^(img_) (jpg|png|gif)$ cannot seem to validate the middle part. Thank you in advance! Match:

img_01.jpg img_02.png img_03.gif img_04.png img_05.gif img_06.jpg

Exclude: mov_01.avi

1 Answer

In case you are still curious, what you typed doesn't validate the middle part because the middle part isn't typed. The ^ includes everything after it, including the (jpg|png|gif), while the $ includes everything before it, including the (img_). Think of the ^ and the $ like quotes in the specific way that you have used it. So you effectively have told the computer to search for literally (img_)(jpg|png|gif) with no exceptions, which means nothing with a "middle part" will show up.

An alternative (and perhaps overly simplified) way to search would be something like ^(img_)....(jpg|png|gif)$

You could also do something like ^(img_)_\d{2}.(jpg|png|gif)$ to be extra specific.

Hope that helps!