1

Is there a good a method out there that will check to see if a string is a relative path

Examples:

path = "/absolute/path/to";

path = "relative/path/to"

path = "http://www.absolutePathAsWellWithoutTrailingSlash.com"

path = "file:///www.absolutePathAsWellWithoutTrailingSlash.com"

You can see that checking just for a start slash won't do, to determine if it is absolute or relative.

Is there a good method for this out there, or should I write it myself?

mjs
  • 21,431
  • 31
  • 118
  • 200

3 Answers3

12

Or you could use a regex:

function isPathAbsolute(path) {
  return /^(?:\/|[a-z]+:\/\/)/.test(path);
}
mjs
  • 21,431
  • 31
  • 118
  • 200
sabof
  • 8,062
  • 4
  • 28
  • 52
  • That looks good. Probably better than relying on the DOM. – Paul Jul 23 '13 at 19:23
  • Great sabof, I was in one the same line ... improvement tip, use test and (?: non grouping, not that matters in test but perhaps it might speed things up :p – mjs Jul 23 '13 at 19:28
  • 1
    I suppose that would be a bit cleaner. – sabof Jul 23 '13 at 19:30
  • Almost! The test method is unfortunately on the RegExp object :) – mjs Jul 23 '13 at 19:33
  • isn't path a String, and since when did Strings have a test method? how can a non-working answer get accepted? – dandavis Jul 23 '13 at 19:33
  • @dandavis haha ... i was 8 seconds quicker :p – mjs Jul 23 '13 at 19:34
  • actually I think double slashes in the beginning might also be considered absolute. I will add it to your answer if that is ok? Sorry, one might also consider adding support for uppercase characters for the protocols... – mjs Jul 23 '13 at 19:54
  • 1
    @Hamidam The regex will match paths beginning with a double slash as it is. – sabof Jul 23 '13 at 20:01
  • @sabof yes that is true, silly me :D – mjs Jul 23 '13 at 20:02
  • Note that URI schemes may contain digits (eg. `ed2k://`) and are not always followed by `://` (eg `mailto:`, `javascript:`). The following pattern should do the job: `/^(?:\/|[a-z0-9]+:)/` – CDuv Jun 30 '15 at 11:40
  • /^(?:\/|[a-z]+:\/\/)/.test("/test/") returns true... – flowest Nov 25 '19 at 13:06
2

Something like this may work (I have not tested this):

function isAbsolute(path){
    var a = document.createElement('a');
    a.href = path;
    return a.host !== location.host || a.protocol !== location.protocol;
}

Note that if the absolute url points to the same domain and protocol as the current page, this will return false.

Paul
  • 139,544
  • 27
  • 275
  • 264
0

This is what I came up with:

new RegExp("^(?:/|.+://)").test(path)

supports all mentioned in the question and:

path = "//same/as/the/protocol.com/abc

mjs
  • 21,431
  • 31
  • 118
  • 200
  • This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post. – Daniel Hedberg Jul 23 '13 at 19:44
  • How so? It returns true if absolute and false if relative – mjs Jul 23 '13 at 19:53
  • 2
    I got the impression that you did not answer your own question but just put something that you had but was not working in an answer instead of in your original question (which then should have been an edit to your question or a comment). I apologize if this is not the case. Please remove the "This is what I have as of now" if this is a working solution, just to avoid future confusion. – Daniel Hedberg Jul 23 '13 at 20:04
  • new RegExp("^(?:/|.+://)").test('http://www.goole.de') will return true... – flowest Nov 26 '19 at 09:27
  • @momomo i meant new RegExp("^(?:/|.+://)").test('http://goole.de') – flowest Nov 26 '19 at 14:39
  • somehow stackoverflow is removing "http://". pass http:// before google.de to the test method – flowest Nov 26 '19 at 14:40
  • @flowest I would say that is an absolute url though? – mjs Dec 20 '21 at 19:10