2

My app is going to work in multiple env, in which i need to get the common value (base url for my app) to work across..

from my window location how to i get certain part from the start..

example :

    http://xxxxx.yyyy.xxxxx.com:14567/yx/someother/foldername/index.html

how can i get only:

http://xxxxx.yyyy.xxxxx.com:14567/yx/

my try :

var base = \w([yx]/)

the base only select yx/ how to get the value in front of this?

this part..

thanks in advance..

2
  • When you say "base url", do you mean a URL you've specified with the <base> element in your HTML? Commented Jul 11, 2013 at 8:44
  • no, the path common for all environments Commented Jul 11, 2013 at 8:44

5 Answers 5

3

If 'someother' is known to be the root of your site, then replace

    \w([yx]/)

with

    (.*\/)someother\/

(note that the / characters are escaped here) which gives a first match of:

   http://xxxxx.yyyy.xxxxx.com:14567/yx/

However, a regular expression may not be the best way of doing this; see if there's any way you can pass the base URL in by another manner, for example from the code running behind the page.

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

Comments

1

If you don't mind disregarding the trailing slash, you can do it without a regex:

var url = 'http://xxxxx.yyyy.xxxxx.com:14567/yx/someother/foldername/index.html';

url.split('/', 4).join('/');
//-> "http://xxxxx.yyyy.xxxxx.com:14567/yx"

If you want the trailing slash, it's easy to append with + '/'.

6 Comments

This doesn't cater for situations where it's running from the root of the site, or under two or more subfolders.
@AdrianWragg what? Your comment makes no sense. Can you give me a real example of where your answer would succeed where my own would fail?
@AdrianWragg: right, I see what you're saying... but this isn't the case for the OP. For his given example, this answer is much clearer than a regex based solution, so does it really warrant the down vote, or are you being a little overzealous?
I don't feel it's useful in the context of his question. Possibly I'm being overzealous; my downvote is locked, but if you want to edit your answer slightly I should be able to remove it.
|
0

Please try following regexp:

http\:\/\/[\w\.]+\:\d+\/\w+\/

Comments

0

This one should do pretty well

http:\/\/[\w\.]+\:\d+\/\w+\/

Comments

0

Perhaps something like this?

Javascript

function myBase(url, baseString) {
    if (url && baseString) {
        var array = url.split(new RegExp("\\b" + baseString + "\\b"));

        if (array.length === 2) {
            return array[0] + baseString + "/";
        }
    }

    return null;
}

var testUrl = "http://xxxxx.yyyy.xxxxx.com:14567/yx/someother/foldername/index.html",
    testBase = "yx";

console.log(myBase(testUrl, testBase))

;

Output

http://xxxxx.yyyy.xxxxx.com:14567/yx/ 

On jsfiddle

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.