2012-12-26 171 views
0

我知道這是荒謬的,但我堅持它截斷字符串在Javascript

我在ASP經典

的filepath是下使用文件上傳文件路徑:\ FakePath \ 3.JPG

我想在一個變量來檢索它,這樣只會給我3.JPG

substring()substr()不includ E 3,我不知道爲什麼

logopath = C:\FakePath\3.jpg; 
logopath = logopath.substring(10); 
+0

請使用'substring()'或'substr()' –

+0

檢查已編輯的文章 –

+0

如果您只想使用JavaScript,請不要使用其他標籤。 –

回答

2

試試這個

'C:\\FakePath\\3.jpg'.split('\\').pop(); // 「3.JPG」

或(正則表達式)

'C:\\FakePath\\3.jpg'.replace(/^.*\\/, ''); // "3.jpg" 

enter image description here

+0

只有單斜槓 –

+1

在_display string_中只有一個斜槓。你怎麼知道'\ t'是'backslash t'還是'tab'?在js你應該使用'特殊字符'。 –

+0

它將永遠是一個反斜槓而不是選項卡 –

1

在你希望使用子串的情況:

var str="C:\\FakePath\\3.jpg"; 
var imgName = str.substring(12); 
+0

請注意,該字符串將需要''\\''。另外,還需要'12'去除最後一個'''''。 – pimvdb

+0

謝謝@pimvdb我在一段時間後意識到它。 –

+0

但是沒有\\只有一個\ –

1
logopath = encodeURIComponent(logopath).replace(/.+FakePath%0/, '') 

「\ 3」被解釋爲指向一個不可打印的ASCII字符一個八進制轉義序列。

1

如果你想解決它在經典的ASP plesae試試這個。

<% 
dim aryPath 
aryPath = Split("C:\FakePath\3.jpg","\") 
Response.Write aryPath(2) 
%> 

希望它有幫助。

+0

OP要求JavaScript;請注意,您可以使用JavaScript或VBScript編寫服務器端的經典ASP。 – webaware

1

使用這樣的代碼:

function FileChanged(input) { 
    var fullPath = input.value; 
    var index = fullPath.lastIndexOf("\\"); 
    var fileName = (index < 0) ? fullPath : fullPath.substr(index + 1); 
    alert(fileName); 
}​ 

中間兩行是你所需要的:最後一個斜線後,他們將採取價值。這樣,路徑是什麼並不重要,它總是隻返回文件名。

Live test case