简体   繁体   中英

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.

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:mail@example.org")); 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")); 

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:mail@example.com')); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/URI.js/1.18.2/URI.min.js"></script> 

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

If you are looking to test all of the anchor tags you can do this. The RegEx patter checks for any typos AKA 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);
}
/**
 * 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);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM