检查路径的正则表达式是相对的还是绝对的

时间:2022-07-05 21:46:55

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.

我在“a”标签上跟踪onclick事件,onclick函数将获得“a”标签的href attr。我想检查href是绝对路径还是相对路径。是否有任何正则表达式来检查它。

and other then this pattern

然后是这种模式

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

which are the other pattern i have to check.

这是我必须检查的其他模式。

5 个解决方案

#1


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.

如果您要测试所有锚标记,可以执行此操作。 RegEx模式检查任何错字A.K.A.胖手指。

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

#2


7  

Here is a solution using URI.js#is:

这是一个使用URI.js的解决方案#是:

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>

#3


6  

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

你为什么要打扰正则表达式?使用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"));

#4


3  

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

示例:https://regex101.com/r/rdox2A/1

#5


-1  

/**
 * 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);
}

#1


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.

如果您要测试所有锚标记,可以执行此操作。 RegEx模式检查任何错字A.K.A.胖手指。

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

#2


7  

Here is a solution using URI.js#is:

这是一个使用URI.js的解决方案#是:

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>

#3


6  

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

你为什么要打扰正则表达式?使用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"));

#4


3  

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

示例:https://regex101.com/r/rdox2A/1

#5


-1  

/**
 * 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);
}