2013-03-17 40 views
2

我在導航樹中有節點。我想選擇所有屬於網址一部分的節點,這樣我就可以突出顯示它們。我有它的工作,但忘了邊緣的情況下,網址的最後一部分以相同的字符串開始。檢查url正則表達式的一部分

在這種情況下,如果有人在url /products/foobar-super我希望它選擇/products/products/foobar-super但不是/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與測試:http://jsfiddle.net/RKDga/2/

不知道是否有可能用正則表達式的其他解決方案,我的猜測是分裂節點和/當前的URL比較這些。

+0

是的!第二個,將URL分開並檢查。它非常簡單,你可以在一個月內回到它,並確切地瞭解它的功能。我建議你自己編寫這段代碼,並自己回答這個問題,我懷疑你可以做得很好。 – 2013-03-17 19:44:22

+1

只是寫了它猜測我被卡住想用這個正則表達式解決這個問題。 – Pickels 2013-03-17 19:47:43

回答

1

測試將通過,如果

new RegExp(node + '.*') 

改爲

new RegExp(node + '(/|$)') 

它可以防止匹配,除非node後跟/或字符串的結尾。

+0

因爲我在我的問題中要求一個正則表達式,我會接受你的答案。這很簡單,我實際上有點尷尬。 – Pickels 2013-03-17 23:16:05

1

下面的代碼可以幫助你JSFIDDLE

/*globals mocha: true, describe: true, it: true, beforeEach:true */ 
(function() { 
    function assert(expr, msg) { 
     if (!expr) throw new Error(msg || 'failed'); 
    } 

    mocha.setup({ 
     ui: "bdd", 
     ignoreLeaks: true 
    }); 

    describe('part of url', function() { 

     it('matches /products and /products/foobar-super', function() { 

     var current = '/products/foobar-super'; 

     var nodes = [ 
      '/products', 
      '/products/foobar', 
      '/products/foobar-super', 
     ]; 

     var result = []; 
     ; 
    var urllist = current.split("/"); 
    var temp; 
    nodes.forEach(function (node) { 
     temp = ""; 
     for(var i=1;i<urllist.length; i++){ 
      temp += "/"+ urllist[i]; 
      if(node == temp){ 
      result.push(node); 
      } 
     } 
    }); 

console.log(result);   

     assert(result[0] === '/products', 'first item should be /products'); 
     assert(result[1] === '/products/foobar-super', 'second item should be /products/foobar-super'); 


     }); 

    }); 


mocha.run(); 
}()); 
+0

你介意解釋你在代碼中改變了什麼,爲什麼? – JJJ 2013-03-17 19:49:08

+0

@Juhana我加了jsfiddle鏈接。 – Anoop 2013-03-17 19:56:38

+0

這很酷,但你介意解釋你在代碼中改變了什麼,爲什麼? – JJJ 2013-03-17 20:50:57

1

這裏有沒有一個正則表達式的其他解決方案:

describe('part of url', function() { 

    it('matches /products and /products/foobar-super', function() { 

    var current = '/products/foobar-super'; 

    var nodes = [ 
     '/products', 
     '/products/foobar', 
     '/products/foobar-super', 
    ]; 

    var result = []; 

    nodes.forEach(function (node) { 

     var node_parts = node.split('/'); 
     var current_parts = current.split('/'); 
     var match = true; 

     node_parts.forEach(function (node_part, i) { 

     if (node_part !== current_parts[i]) { 
      match = false; 
     } 

     }); 

     if (match) { 
     result.push(node); 
     } 

    }); 

    result.should.eql([ 
     '/products', 
     '/products/foobar-super', 
    ]); 

    }); 

}); 
+1

如果這是你最終使用它,如果你能解釋你的代碼並接受你的答案,那將是非常好的。 – 2013-03-17 20:37:18

+0

@BenjaminGruenbaum在問題的兩天內你不能接受你自己的答案。 – Pickels 2013-03-17 23:11:07