JSFiddle - React, Tailwind, and code Playground
by r3wt
JavaScript
var cases = [
'/bar/test/foo.js',
'https://google.com/bar/test/foo.js',
'./foo.js',
'bar/test/foo.js',
'foo.js',
'http://foo.com/foo.js'
];
function extractPath(s){
s = s.replace(/^https?:\/\/[^\/]+\//,'');
s = s.replace('./','');
s = s.split('/');
s.pop();
if(s.length > 0){
s = '/'+ s.join('/') + '/';
}else{
s = '/';
}
s = s.replace('//','/');
return s;
}
function testAll()
{
for(var i=0;i<cases.length;i++)
{
testI(i);
}
}
function testI(i)
{
console.log(extractPath(cases[i]));
}
testAll();