2013-03-06 56 views
0
$(function() { 
    var s = $('#field_id5 div.field_uneditable').html(); 
    if(s == "+Member") { 
     var path = window.location.pathname; 
if(/^u\d+/.test(path)){ 
    $('#content').prependTo('#container').css('marginRight','0px'); 
    $('#page_wrapper').remove(); 
    } 
$('#profile-advance-left').hide(); 
    } 
}); 

得到錯誤未捕獲的ReferenceError:路徑沒有爲var numbers定義搞清楚,如果路徑帶u開始

更新的代碼---以上上面

的代碼,我試着寫一個正則表達式的排序(在正則表達式中不是很有經驗),它會檢查數字是否在u之後。代碼的工作原理除了檢查u是否在路徑名中。我只希望它檢查是否以u開頭,因爲我有一個類似/forum這樣的鏈接,並且這個代碼被運行,這不是我想要的。我需要這個代碼,像這樣

/u1

/u2

/u3

/u4

任何建議網址上只運行?

好了,所以下面兩個答案都仍然沒有工作..我的一個朋友給了我一個代碼,但我不認爲這是逃出請看看

if(//?ud+/.test(window.location.pathname)){ 
    $('#content').prependTo('#container').css('marginRight','0px'); 
    $('#page_wrapper').remove(); 
} 

回答

2

I only want it to check if starts with u

if(window.location.pathname.indexOf('u') == 0) ... 

if(/^u/.test(window.location.pathname))... 

如果您需要確保u後跟1個或多個數字,我會做到這一點:

if(/^u\d+/.test(window.location.pathname)) ... 

完整代碼段是:

if(/^u\d+/.test(window.location.pathname)){ 
    $('#content').prependTo('#container').css('marginRight','0px'); 
    $('#page_wrapper').remove(); 
} 
+0

Hey Kyle,.test是否需要在正則表達式之後?我也應該擺脫'var checkif =路徑名+數字'? – EasyBB 2013-03-06 14:29:50

+0

我現在有這個權利... 'var pathname = window.location.pathname;' 'var regex =/^ u [0-9] * /;' 'var reg = regex.test(pathname) ;' 'if(reg){// code following' – EasyBB 2013-03-06 14:34:11

+0

查看我的編輯,這是你所需要的。 – Kyle 2013-03-06 14:35:01

0
var regex = /^u[0-9][0-9]*/; 
regex.test(checkif); 

這是檢驗它的格式是 「U」 +系列數字

編輯:按照固定的正則表達式功能凱爾的評論。這可能有一個更簡潔的方法,但它應該起作用。

+2

這將匹配「u」。 '*'允許零個或多個數字。另外,不需要爲'u'字符設置字符類。 '/^u [0-9] * /'與您擁有的相同。 – Kyle 2013-03-06 14:26:30

+0

好的安東尼,因爲我是新的,不寫正則表達式。你能解釋手頭的代碼嗎? '//'通常是正確寫入正則表達式的地方? '^'是什麼開始或包含? '[u]'是我正在尋找的,然後是'[0-9]','*'是什麼? – EasyBB 2013-03-06 14:26:44

+0

@凱爾:啊,是的,這是真的。好決定。修復。 – 2013-03-06 15:13:28