Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

compare path and get relative path between two files with javascript

Tags:

javascript

How compare dynamically two paths in the same domain and get the relative path between them?

var path2 = "http://site.net/test1/test2/img/1.jpg" // test example
var path3 = "http://site.net/test1/img/1.jpg" // test example

And get a return path2 to path3 for example = "../../img/"

like image 664
Sergio Avatar asked Aug 29 '26 05:08

Sergio


1 Answers

A better way would be to use path.relative

const path = require('path');
const path2 = "http://example.com/test1/test2/img/1.jpg";
const path3 = "http://example.com/test1/img/1.jpg";

const relativePath = path.relative(path.dirname(path2),path.dirname(path3));
console.log(relativePath); //'../../img'
like image 162
noob Avatar answered Aug 31 '26 18:08

noob



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!