最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - regex to check path is relative or absolute - Stack Overflow

programmeradmin2浏览0评论

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"
""
""

which are the other pattern i have to check.

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.

Share Improve this question asked Oct 19, 2016 at 7:28 Ashish PatelAshish Patel 9411 gold badge10 silver badges28 bronze badges 2
  • 1 It might be duplicate of stackoverflow.com/questions/2406739/… – priya_singh Commented Oct 19, 2016 at 7:30
  • Note that most answers here don't take into account protocol-less URLs. – mcont Commented May 17, 2022 at 14:21
Add a comment  | 

5 Answers 5

Reset to default 7

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>

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"));

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 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);
}
/**
 * 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);
}
发布评论

评论列表(0)

  1. 暂无评论