xhallix
xhallix

Reputation: 3011

cutting off string after last word in dynamic url

what I'm trying to do is:

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

I need to cutoff the string right after "cutoff". The link will change quiet often, so I need do to something like:

regexp = /cutoff/i (everything which comes after this) window.location = regexp;

The link does also change at the beginning, therefore it is not possible to count the char and replace it then.

Thank you for any answer given to this.

Upvotes: 0

Views: 378

Answers (3)

Michael Sazonov
Michael Sazonov

Reputation: 1533

Try

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

Upvotes: 0

Emil L
Emil L

Reputation: 21071

Is this too simple a solution?

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

Upvotes: 0

MattDiamant
MattDiamant

Reputation: 8771

Everything up until cutoff/?

/.*cutoff\//

Edit: Here's the 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/

Upvotes: 1

Related Questions