2

I have the following path :

/data/2/444/test.text

or (without a slash at the start of the path) data/2/444/test.text

I would like to return in JS the following result :

   "/2/444/test.text"

I tried with the following: but I managed only to get the base name

new String(str).substring(str.lastIndexOf('/') + 1);
3
  • How generic should it be? Is the path always starting with /data/data? Commented Apr 21, 2018 at 21:32
  • 1
    Sometimes it's from the first slash, but sometimes (as you describe this) it's starting from the second slash. What would determine starting from the first or second? Commented Apr 21, 2018 at 21:33
  • 2
    How about str.split('/data')[1] ? Commented Apr 21, 2018 at 21:33

4 Answers 4

4

You can use a simple regex to remove the first directory in the path.

str.replace(/^\/?[^/]+\//, '/')
  • ^\/? Optional slash at the beginning of string.
  • [^/]+\// match any non slash character until it encounter a slash

const input = ['/data/2/444/test.text', 'data/2/444/test.text', 'file.txt'];

const output = input.map(str => str.replace(/^\/?[^/]+\//, '/'))
 
console.log(output);

If you only want to replace /data from the beginning you can use:

^\/?data\/
Sign up to request clarification or add additional context in comments.

Comments

1

lastIndexOf finds the last occurrence of a string within a string. When you use substring(x) on a string y, it will return the characters of y starting at x. So using lastIndexOf in this use case isn't what you want. You can achieve what you want by using indexOf (finding the first occurrence of a string within a string). To account for the different formats of your input string (i.e. /data and data), you can just test for that:

function getPathWithoutData(str) {
    var strWithoutSlash = str[0] === '/' ? str.substring(1) : str;
    return strWithoutSlash.substring(strWithoutSlash.indexOf('/'));
}

Comments

1

You can easily do it without regexes and using slice and indexOf:

const getPath=path=>path.slice(path.indexOf('/',path[0]==="/"?1:0)-path.length);

console.log(getPath('/data/2/444/test.text'));
console.log(getPath('data/2/444/test.text'));

This checks if the first char is a / or not, and adjusts the indexOf accordingly to match either the second or first /. Also note how the subtraction gives a negative value, which gets the intended characters from the end, up to the /.

Of course you can do still do it with substring, as you were attempting, but with indexOf instead of lastIndexOf, because you want the 1st or 2nd / not the last one:

const getPath=path=>path.substring(path.indexOf('/',path[0]==="/"?1:0),path.length);

console.log(getPath('/data/2/444/test.text'));
console.log(getPath('data/2/444/test.text'));

It's worth mentioning that these may not be as robust as a regex, but are simple enough, and may fit your needs, depending on how the data can vary.

Comments

0

You could use String.prototype.split() and pass it a regex.

const paths = [
  "/data/path/one",
  "data/path/two"
];
const modifyPath = p => {
  const [fallback, newPath] = p.split(/\/?data/);
  return newPath || fallback;
}
console.log(paths.map(modifyPath));

Or you could use String.prototype.replace()

const paths = [
  "/data/path/one",
  "data/path/two",
];
const modifyPath = p => {
  return p.replace(/\/?data(.*)/, '$1');
}
console.log(paths.map(modifyPath));

1 Comment

Have in mind if data isn't present in the path this will yield undefined.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.