Split string of folder path

This is no specialized version of split per se, but you can split by the path.sep like so:

import path from 'path';

filePath.split(path.sep);


use the split->slice->join function:

"var/www/parent/folder".split( '/' ).slice( 0, -1 ).join( '/' );

Use the following regular expression to match the last directory part, and replace it with empty string.

/\/[^\/]+$/

'var/www/parent/folder'.replace(/\/[^\/]+$/, '')
// => "var/www/parent"

UPDATE

If the path ends with /, the above expression will not match the path. If you want to remove the last part of the such path, you need to use folloiwng pattern (to match optional last /):

'var/www/parent/folder/'.replace(/\/[^\/]+\/?$/, '')
// => "var/www/parent"