1

When googling, I see lots of examples like this:

App.Router = Backbone.Router.extend({
  routes: {
      '': 'index',
      'show/:id': 'show'
  },

  index: function(){
      $(document.body).append("Index route has been called..");
  },
  show: function(id){
      $(document.body).append("Show route with id: "   id);
  }
});

How would this implementation look like using regex?

I want something like:

App.Router = Backbone.Router.extend({
  routes: {
      '': 'index',
      /show/(\d+:id)/: 'show'
      /show/([A-Za-z]+:other)/: 'showSpecial'
  },

where the first regex matches /show/[any number] and passes that number in the id parameter to the show function.

and where the second regex matches /show/[any word] and passes that word in the other parameter to the showSpecial function.

Cotten
  • 8,787
  • 17
  • 61
  • 98

1 Answers1

2

I don't believe this syntax will work:

App.Router = Backbone.Router.extend({
  routes: {
      '': 'index',
      /show/(\d+:id)/: 'show'
      /show/([A-Za-z]+:other)/: 'showSpecial'
},

Instead you can write it like this:

App.Router = Backbone.Router.extend({
  initialize: function() {
      this.route(/^$/, 'index');
      this.route(/^show\/(\d+)$/,"show");
      this.route(/^show\/([A-Za-z]+)/, "showSpecial");
   }
})
jarv
  • 5,338
  • 24
  • 26