0

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=?

1
  • Is the URL always of the form *preview/*? (* means any amount of characters) Commented Oct 18, 2013 at 10:45

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.