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

Piotr Manczak
.a{fill-rule:evenodd;}techdegree seal-36
Piotr Manczak
Front End Web Development Techdegree Graduate 28,940 Points

Why does it work? I tried to match regular expressions.

Why does it work? I tried to match regular expressions: www.github.com github.com www.teamtreehouse.com teamtreehouse.com api.github.com

and exclude those: jsfiddle.net www.jsfiddle.net

I used: ((www.)|(api.)|)(github|teamtreehouse|).com This works but I am not so sure why. Especially this: ((www.)|(api.)|). Joel did not mention in his video case like this. Does it mean www. OR api. OR nothing at all?

1 Answer

Michael Hulet
Michael Hulet
47,911 Points

In JavaScript regular expressions, the vertical bar (|) acts as a logical OR. The subdomain part of your regular expression (((www.)|(api.)|)) will match 3 scenarios:

  1. The string starts with www.
  2. The string starts with api.
  3. The first character in the string is an empty string (in other words, the string is only either github.com or teamtreehouse.com)