Merge pull request #804 from davidmurdoch/patch-1

Optimize regular expressions.
This commit is contained in:
Jeremy Ashkenas
2011-12-20 15:51:27 -08:00

View File

@@ -638,8 +638,8 @@
// Cached regular expressions for matching named param parts and splatted
// parts of route strings.
var namedParam = /:([\w\d]+)/g;
var splatParam = /\*([\w\d]+)/g;
var namedParam = /:[\w\d]+/g;
var splatParam = /\*[\w\d]+/g;
var escapeRegExp = /[-[\]{}()+?.,\\^$|#\s]/g;
// Set up all inheritable **Backbone.Router** properties and methods.
@@ -687,9 +687,9 @@
// Convert a route string into a regular expression, suitable for matching
// against the current location hash.
_routeToRegExp : function(route) {
route = route.replace(escapeRegExp, "\\$&")
.replace(namedParam, "([^\/]*)")
.replace(splatParam, "(.*?)");
route = route.replace(escapeRegExp, '\\$&')
.replace(namedParam, '([^\/]*)')
.replace(splatParam, '(.*?)');
return new RegExp('^' + route + '$');
},
@@ -855,7 +855,7 @@
// a new one to the browser history.
_updateHash: function(location, fragment, replace) {
if (replace) {
location.replace(location.toString().replace(/(javascript:|#).*$/, "") + "#" + fragment);
location.replace(location.toString().replace(/(javascript:|#).*$/, '') + '#' + fragment);
} else {
location.hash = fragment;
}