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

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,842 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)

Thank you for taking your time to answer that. I suspected that could be the case but I wanted to be sure. Thanks again.