Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 686
Ashish Patel Avatar asked Oct 19 '16 07:10

Ashish Patel


1 Answers

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>
like image 71
Yosvel Quintero Arguelles Avatar answered Sep 30 '22 17:09

Yosvel Quintero Arguelles