"/siparis/kampanyalar/tumu/cok-karisik-orta-firsati"
сделать такую "/siparis/kampanyalar/tumu"
?"/siparis/kampanyalar/tumu/karisik-ort"
такую "/siparis/kampanyalar/tumu"
?/
и включая сам слэш? const expression = /.+(?=\/)/;
const array = [
'/siparis/kampanyalar/tumu/cok-karisik-orta-firsati',
'/siparis/kampanyalar/tumu/karisik-ort',
'/siparis'
];
for (const entry of array) {
const match = entry.match(expression);
const parsed = match !== null ? match[0] : null;
console.log(parsed);
}
/*
'/siparis/kampanyalar/tumu'
'/siparis/kampanyalar/tumu'
null
*/
const dirname = (str) => {
const pos = str.lastIndexOf('/');
if (-1 === pos) return str;
return str.substring(0, pos);
}
dirname('/siparis/kampanyalar/tumu/cok-karisik-orta-firsati')
// /siparis/kampanyalar/tumu