Ashish Patel
Ashish Patel

Reputation: 941

regex to check path is relative or absolute

I am tracking onclick event on "a" tag , onclick function will get the href attr of "a" tag . i want to check the href is absolute path or relative path. is there any regex expression to check it.

and other then this pattern

"/myfolder/test.txt"
"http://example.com"
"https://example.com"

which are the other pattern i have to check.

Upvotes: 5

Views: 3519

Answers (5)

Sébastien
Sébastien

Reputation: 1968

/**
 * Return true is the URL is absolute
 * 
 * @export
 * @param {string} url 
 * @returns
 */
export function isURLAbsolute(url) {
    if (typeof url !== 'string') {
        throw new TypeError('Expected a string');
    }
    return /^[a-z][a-z0-9+.-]*:/.test(url);
}

Upvotes: -1

Thomas Ayoub
Thomas Ayoub

Reputation: 29451

Why do you bother with a regex? Use str.startsWith:

console.log("/myfolder/test.txt".startsWith("/"));
console.log("http://example.com".startsWith("/"));
console.log("https://example.com".startsWith("/"));


If you need to consider the case of paths starting with / or ~:

console.log(/^([?/~]|mailto.*@.*\.\w+$)/.test("mailto:[email protected]"));
console.log(/^([?/~]|mailto.*@.*\.\w+$)/.test("~/myfolder/test.txt"));
console.log(/^([?/~]|mailto.*@.*\.\w+$)/.test("/myfolder/test.txt"));
console.log(/^([?/~]|mailto.*@.*\.\w+$)/.test("http://example.com"));
console.log(/^([?/~]|mailto.*@.*\.\w+$)/.test("https://example.com"));

Upvotes: 7

Yosvel Quintero
Yosvel Quintero

Reputation: 19090

Here is a solution using URI.js#is:

function isAbsoluteUri(str) {
  var uri = new URI(str);
  return uri.is('absolute');
}

console.log(isAbsoluteUri('/myfolder/test.txt'));
console.log(isAbsoluteUri('~/myfolder/test.txt'));
console.log(isAbsoluteUri('?hello=world'));
console.log(isAbsoluteUri('http://example.com'));
console.log(isAbsoluteUri('https://example.com'));
console.log(isAbsoluteUri('ftp://example.com'));
console.log(isAbsoluteUri('mailto:[email protected]'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/URI.js/1.18.2/URI.min.js"></script>

Upvotes: 7

andre mcgruder
andre mcgruder

Reputation: 1520

If you are looking to test all of the anchor tags you can do this. The RegEx patter checks for any typos A.K.A. fat fingers.

function checkRelpath(){
    var anChrs = $('a');

    $.each(anChrs, function(idx, itm){
        var pattern = new RegExp('/^(http|https|http:|https:|\/\/)/'),
            path = $(itm).prop('href'),
            testPath = pattern.test(path);

        console.log(path);
        console.log(testPath);
    });
}

Or if you want to test a specific anchor you could do this.

function checkThisRelpath(a){
    var pattern = new RegExp('/^(http|https|http:|https:|\/\/)/'),
        path = $(a).prop('href'),
        testPath = pattern.test(path);

        console.log(path);
        console.log(testPath);
}

Upvotes: 1

Addison
Addison

Reputation: 8407

If you know you're always getting a valid path, then all you really have to do is check if it starts with a /. This should do it:

^\/.*

Example: https://regex101.com/r/rdox2A/1

Upvotes: 4

Related Questions