2011-01-25 119 views
1

我需要在的末尾指定用通配符路徑if語句.. 像*的文件系統中的等價通配符在javascript路徑

if(document.location.pathname == "/project/*") { jQuery("#regbutton").css('display','none'); }; 

即..中頻應該火的所有路徑開始/項目/

任何方式做到這一點瓦特/出訴訴野生正則表達式的東西?
jQuery的解決方案將是偉大的太..

日Thnx

回答

4

它使用正則表達式,但它是我們所馴服的正則表達式得到。

if(document.location.pathname.match(/^\/project\//)) 
{ 
    jQuery("#regbutton").css('display','none'); 
} 
+0

工作流暢如絲..多thanx(這真的是非可怕的正則表達式:) – Joseph 2011-01-25 03:45:56

+0

@約瑟夫:我編輯了我的答案;我忘了你不需要在`location.pathname`上調用`toString()`(因爲它已經是一個字符串了)[儘管你需要在``location`本身上調用`toString()`](https: //developer.mozilla.org/en/DOM/window.location#Use_as_a_string)。 – 2011-01-25 03:55:08

0

你可以做這樣的事情: -

String.prototype.startsWith = function(str) {return (this.match("^"+str)==str)} 

if(document.location.pathname.startsWith("/project/")) { 
    ... 
} 

這樣,您就可以繼續使用startsWith()函數,只要你願意下一次。