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.
/data/data?str.split('/data')[1]?