Use the following regular expression to match the last part of the directory and replace it with an 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 such a path, you need to use the folloiwng pattern (to match the optional last /
):
'var/www/parent/folder/'.replace(/\/[^\/]+\/?$/, '') // => "var/www/parent"
source share