I have nodes in a navigation tree. I want to select all the nodes that are part of the url so I can highlight them. I had it working but forgot an edge case where the last part of the url starts with the same string.
In this case if somebody is at the url /products/foobar-super I want it to select /products and /products/foobar-super but not /products/foobar.
describe('part of url', function () {
it('matches /products and /products/foobar-super', function () {
var current = '/producten/foobar-super';
var nodes = [
'/products',
'/products/foobar',
'/products/foobar-super',
];
var result = [];
nodes.forEach(function (node) {
if (new RegExp(node + '.*').test(current)) {
result.push(node);
}
});
result.should.eql([
'/products',
'/products/foobar-super',
]);
});
});
Jsfiddle with the test: http://jsfiddle.net/RKDga/2/
Not sure if it is possible with a regex the other solution I guess is to split the node and the current url on / compare those.