Nigina Sharipova
Nigina Sharipova

Reputation: 13

Split url with javascript from position

I need to parse a url:

http://localhost.com/kw-webapp/preview/cn/1/cmm/Default/13203/content.13203.1.1.html?context=

var url = window.location.pathname.split( '/' );

                var _language = 4;
                var _layoutuid = 5;
                var _themeuid = 6;
                var _contentuid = 7;
                var _content = 8;

                var language = url[_language];
                var layoutuid = url[_layoutuid];
                var themeuid = url[_themeuid];
                var contentuid = url[_contentuid];
                var content = url[_content];

But I need to parse the url from this position:

cn/1/cmm/Default/13203/content.13203.1.1.html?context=

and variables have to be:

var _language = 0;  //1
                var _layoutuid = 1; //cmm
                var _themeuid = 2;  //Default
                var _contentuid = 3;    //13203
                var _content = 4;

My problem is, url can start like this:

www.localhost.com/kw-webapp/kw/preview/cn/1/cmm/Default/13203/content.13203.1.1.html?context=

or this:

www.localhost.com/preview/cn/1/cmm/Default/13203/content.13203.1.1.html?context=

How can I parse the url from cn/1/cmm/Default/13203/content.13203.1.1.html?context=?

Upvotes: 0

Views: 382

Answers (1)

Harry
Harry

Reputation: 89770

If your URL is always of the form *preview/cn/* and you want the last part of it, you can trim the URL using RegExp like below:

var trimmedURL = "http://localhost.com/kw-webapp/preview/cn/1/cmm/Default/13203/content.13203.1.1.html?context=".replace(/.*preview\/cn\//,'');
//console.log(trimmedURL);
var url = trimmedURL.split( '/' );

Demo Fiddle

Upvotes: 1

Related Questions