简体   繁体   English

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

[英]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 . 我在“a”标签上跟踪onclick事件,onclick函数将获得“a”标签的href attr。 i want to check the href is absolute path or relative path. 我想检查href是绝对路径还是相对路径。 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 : 使用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 : 这是一个使用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> 

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

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. RegEx模式检查任何错字AKA胖手指。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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