How to test if a URL string is absolute or relative?

check if url is absolute c#
javascript url-builder
javascript url-join
check if string is-url javascript
relative url
javascript check if url is https
new url relative path
javascript parse url

How can I test a URL if it is a relative or absolute path in Javascript or jQuery? I want to handle accordingly depending if the passed in URL is a local or external path.

if (urlString starts with http:// or https://)
 //do this
var pat = /^https?:\/\//i;
if (pat.test(urlString))
{
    //do stuff
}

For protocol relative urls, use this regex:

/^https?:\/\/|^\/\//i

JavaScript, indexOf() method to get to know if the position of “://” has index greater than 0 or position of “//” has index equal to 0. Both these condition check leads us to the absolute URL. Example 2: This example uses the approach discussed above. Test if a URL string is absolute or relative? The task is to check if the passed URL is absolute or relative. Below are the few approaches: Approach 1: Use Regular Expression which checks if the URL contains “//” at a position in the URL . Example 1: This example uses the approach discussed above.

FAST

If you only need to test for http:// or https:// then the most efficient way is:

if (urlString.indexOf('http://') === 0 || urlString.indexOf('https://') === 0)

UNIVERSAL

However, I would suggest a more universal, non case-sensitive, protocol-agnostic approach:

var r = new RegExp('^(?:[a-z]+:)?//', 'i');
r.test('http://example.com'); // true - regular http absolute URL
r.test('HTTP://EXAMPLE.COM'); // true - HTTP upper-case absolute URL
r.test('https://www.exmaple.com'); // true - secure http absolute URL
r.test('ftp://example.com/file.txt'); // true - file transfer absolute URL
r.test('//cdn.example.com/lib.js'); // true - protocol-relative absolute URL
r.test('/myfolder/test.txt'); // false - relative URL
r.test('test'); // false - also relative URL

Explain the RegExp
^(?:[a-z]+:)?//

^ - beginning of the string (?: - beginning of a non-captured group [a-z]+ - any character of 'a' to 'z' 1 or more times : - string (colon character) )? - end of the non-captured group. Group appearing 0 or 1 times // - string (two forward slash characters) 'i' - non case-sensitive flag

Convert relative path URL to absolute path URL using JavaScript , Given a relative URL, the task is to convert the relative URL to an absolute URL. Node.js | path.relative() Method · JavaScript | Check whether a URL string is  First, I have read this question about how to check if string is an absolute or relative URL. My problem is I need a regex to check if a given string is a relative URL or not, i.e. I need a regex to check if a string does not start with any protocol or double slash //.

URL(), If the component parameter is specified, parse_url() returns a string (or an integer​, in the case of PHP_URL_PORT ) This function may not give correct results for relative URLs. Test if URL is already absolute (contains host, or begins with '/') How to determine the absolute path to the root of the website / web service in the Global.asax Roel van Lisdonk Uncategorized November 15, 2011 November 15, 2011 If you want to determine if a string contains a relative or absolute folder / file path, you can use the System.IO.Path.IsPathRooted function:

Use a regex:

if (/^(?:[a-z]+:)?\/\//i.test(url))

parse_url - Manual, Constructor: new URL(input[, base]); url.hash; url.host; url.hostname; url.href; url.​origin; url. url.format(urlObject); url.parse(urlString[, parseQueryString[, slashesDenoteHost]]); url.resolve(from, to) The url module provides utilities for URL resolution and parsing. input <string> The absolute or relative input URL to parse. If you're checking to see if the URL will send the user to a different domain this fails. It doesn't handle protocol-relative URLs. //gmail.com is considered relative, not absolute, to System.Uri.

Even more Universal RFC-compliant URI approach:

(?:^[a-z][a-z0-9+.-]*:|\/\/) regex explanation

The other solutions listed here would fail for links like mailto:evan@nylas.com

RFC 3986 defines a Scheme as:

scheme = ALPHA *( ALPHA / DIGIT / "+" / "-" / "." )

3.1. Scheme https://tools.ietf.org/html/rfc3986#section-3.1

While the protocol-relative url is technically valid as per section 4.2, Paul Irish has swung back the other way and considers this an anti-pattern. See http://www.paulirish.com/2010/the-protocol-relative-url/

4.2. Relative Reference http://tools.ietf.org/html/rfc3986#section-4.2

If you'd like the regex without protocol-relative url's use:

^[a-z][a-z0-9+.-]*:

To see a full list of other types of valid uri edge cases, check out the list here: https://en.wikipedia.org/wiki/URI_scheme

URL, URL object by using its constructor and passing in a String representing the human We can see this in a test: ? However if the relative URL is detected to be absolute in its component parts, then the baseURL is ignored: ? Maybe you're thinking of relative paths in the sense of http URLs? The Node.js utility path.isAbsolute is meant to be used for filesystem paths where an "absolute path" by definition starts with a slash.

A Simple Guide to the Java URL, Determines whether the specified file exists. public static bool Exists (string path); The path parameter is permitted to specify relative or absolute path The Exists method returns false if any error occurs while trying to determine if the​  The System.Uri class in .NET provides a way to validate if a URL is an absolute URL or not. This can be quite handy when your script deals with downloading content from web pages and there is a need to validate the specified URL. The IsWellFormedUriString method provides this capability.

File.Exists(String) Method (System.IO), Q: How do I convert my URLs to relative, absolute, or absolute with domain? Relative and convert_urls options to determine whether TinyMCE returns relative or absolute URLs. This function should return the converted URL as a string. Absolute URL contains more information than a relative URL, but using relative URLs are much easier since they are shorter and more portable. But relative URLs can only be used to refer to links that reside on the same server as the page that refers them.

URL handling options, The syntax for relative URLs is a shortened form of that for absolute URLs [2], the base URL for that message is the string "http://www.ics.uci.edu/Test/a/b/c". To easily convert URLs between absolute and relative, or just ensure these two formats, I created extension-methods for the type System.Uri, which allows you to write code like this:

Comments
  • 'somestring'.indexOf() or by using regex ;]
  • This answers the asked question but you may also want to consider protocol relative urls which start with //.
  • What if url contains "file://"? BOOM! Tragedy. Answer from @Philipp is more reliable.
  • The accepted answer is invalid, at least in 2019. Chrome happily accepts http:example.com.
  • why a-z ? doesn't domain name can have 0-9 and hyphen in domain name?
  • right, but we are not checking for domain name here, are we? This will still work: /^(?:[a-z]+:)?\/\//i.test('https://www.ex-maple-123.com');
  • Can scheme contain digits? We all know http, https, ftp, and mailto. Anyone define custom schemes for internal tools? I think OneNote and Outlook do on Windows.
  • This doesn't account for browser behavior. The browser will remove leading spaces. Start with trim?
  • This does not capture "mailto:" URLs. Not that I know if mailto URLs are absolute or relative ;-)
  • Nope. I was just tracking down a bug in my project and discovered it was too such a function. The webpage had a url like /redirect?target=http://example.org
  • @BeniBela, you could fix this by using function isUrlAbsolute(url) { var firstSlash = url.indexOf('/'); var colonDoubleSlash = url.indexOf('://'); return ((firstSlash > 0 && colonDoubleSlash > 0 && colonDoubleSlash < firstSlash) || url.indexOf('//') === 0); }