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.
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With