切断动态网址中最后一个单词后的字符串

时间:2013-03-13 15:39:03

标签: javascript regex

我要做的是:

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

我需要在“截止”之后立即切断字符串。 链接会经常变得安静,所以我需要这样做:

regexp = / cutoff / i(此后的所有内容) window.location = regexp;

链接在开头也会发生变化,因此无法计算char并将其替换。

感谢您给予的任何答复。

3 个答案:

答案 0 :(得分: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/

答案 1 :(得分:0)

这个解决方案太简单了吗?

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

答案 2 :(得分:0)

尝试

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