diff --git a/src/urlMatcherFactory.js b/src/urlMatcherFactory.js index a16e728c5..cc8e28752 100644 --- a/src/urlMatcherFactory.js +++ b/src/urlMatcherFactory.js @@ -98,13 +98,13 @@ function UrlMatcher(pattern, config, parentMatcher) { return params[id]; } - function quoteRegExp(string, pattern, squash) { + function quoteRegExp(string, pattern, squash, optional) { var surroundPattern = ['',''], result = string.replace(/[\\\[\]\^$*+?.()|{}]/g, "\\$&"); if (!pattern) return result; switch(squash) { - case false: surroundPattern = ['(', ')']; break; + case false: surroundPattern = ['(', ')' + (optional ? "?" : "")]; break; case true: surroundPattern = ['?(', ')?']; break; - default: surroundPattern = ['(' + squash + "|", ')?']; break; + default: surroundPattern = ['(' + squash + "|", ')?']; break; } return result + surroundPattern[0] + pattern + surroundPattern[1]; } @@ -131,7 +131,7 @@ function UrlMatcher(pattern, config, parentMatcher) { if (p.segment.indexOf('?') >= 0) break; // we're into the search part param = addParameter(p.id, p.type, p.cfg, "path"); - compiled += quoteRegExp(p.segment, param.type.pattern.source, param.squash); + compiled += quoteRegExp(p.segment, param.type.pattern.source, param.squash, param.isOptional); segments.push(p.segment); last = placeholder.lastIndex; } diff --git a/test/urlMatcherFactorySpec.js b/test/urlMatcherFactorySpec.js index 5a2e7dc7d..3f45a2156 100755 --- a/test/urlMatcherFactorySpec.js +++ b/test/urlMatcherFactorySpec.js @@ -645,6 +645,14 @@ describe("urlMatcherFactory", function () { expect(m.exec('/users/bar/2')).toBeNull(); }); + it("should populate even if the regexp requires 1 or more chars", function() { + var m = new UrlMatcher('/record/{appId}/{recordId:[0-9a-fA-F]{10,24}}', { + params: { appId: null, recordId: null } + }); + expect(m.exec("/record/546a3e4dd273c60780e35df3/")) + .toEqual({ appId: "546a3e4dd273c60780e35df3", recordId: null }); + }); + it("should allow shorthand definitions", function() { var m = new UrlMatcher('/foo/:foo', { params: { foo: "bar" }