title: Simple JavaScript URL Routing RegEx
body: <p>I'm trying to write a very basic URL router. I define a "routes" object that contains location paths and corresponding "module names."</p>
  
  <p>Upon a full page load (no hash or pushState needed in this scenario), I want to look at the routes and find the corresponding module value, if it exists.</p>
  
  <p>I'm having some trouble with my code.</p>
  
  <pre><code>(function() {
  
      return {
  
          routes: {
              '/': 'home',
              '/about': 'about',
              '/about/team': 'aboutTeam',
              '/about/team/:member': 'aboutMember'
          },
  
          init: function() {
  
              var url = location.pathname;
  
              for (var route in this.routes) {
  
                  if (this.routes.hasOwnProperty(route)) {
  
                      if (url.match(this.regex(route))) {
  
                          // return &amp; load module
                      }
                  }
              }
          },
  
          regex: function(route) {
  
              return new RegExp(route.replace(/:[^\s/]+/g, '([\\w-]+)'));
          }
  
      }.init();
  
  })();
  </code></pre>
  
  <p>The above code doesn't work well at all. (Obviously.) Landing on the page <strong><em>/about/team/jim</em></strong> will return all four pre-defined routes in this example. (And I can see why, just struggling with a solution.)</p>
  
  <p>I'm not great with regular expressions. I snagged what I have above from here: <a href="https://stackoverflow.com/questions/11805441/javascript-routing-regex">Javascript routing regex</a>. I'm looking for advice and guidance. Am I even taking the right approach, overall?</p>