2013-03-13 37 views
0

什麼,我試圖做的是:切斷串最後一個字後,在動態URL

www.test.de/testA/testB/testC/cutoff/testD/testE

我需要截斷「切斷」後的字符串。 鏈接將改變往往安靜,所以我需要做的是這樣的:

正則表達式= /截止/ I(其中後這一切都自帶) 了window.location =正則表達式;

鏈接在開始時也會發生變化,因此無法對char進行計數並將其替換。

謝謝你給出的任何答案。

回答

1

一切都要等到截斷/?

/.*cutoff\//

編輯:這裏的JS

var s = 'www.test.de/testA/testB/testC/cutoff/testD/testE'; 
var x = s.replace(/(.*cutoff\/).*/, '$1'); 
window.location = x; 

http://jsfiddle.net/6F44a/

+0

所以我想這將是 變種S = window.location的 變種X = s.replace(/(.*截止\ /)* /,「$ 1 「); window.location = x ? 或者我怎麼才能得到的網址? :) – xhallix 2013-03-13 16:02:47

+0

你能解釋一下'$ 1'在取代什麼? – xhallix 2013-03-13 16:03:26

+0

確定了:)謝謝! – xhallix 2013-03-13 16:07:05

0

這是太簡單的解決方案?

var s = "www.test.de/testA/testB/testC/cutoff/testD/testE" 
var re = /(.+cutoff)/i 
s.replace(re, "") 
0

嘗試

var reg = new RegExp("([.\/]*\/" + cutoff + "\/)." , 'i'); 
window.location = window.location.replace(reg , "$1"); 
相關問題