Say I have a string for the full path of a directory

let myPath = '/go/src/github.com/MyDomain/MyProject'

I would like to have an array like

['go', 'src', 'github.com', 'MyDomain', 'MyProject']

What is the best way of doing that ? I would prefer to make this platform agnostic. So if there is a split() function tailored for paths, I would prefer that.

Otherwise I think

let out = split(myPath, '/') 

Should do the trick ?

  • Be aware of the UNC pathes (\\server\share\path\to\file) on Windows.
    – Ralf
    Commented Jun 8, 2020 at 6:37

1 Answer 1


I would use regular split function with regex covering 2 path separators.

Windows can use / or \, plus mix of both:

echo split('/go/src/github.com/MyDomain/MyProject', '[/\\]')
echo split('C:\go/src/github.com/MyDomain/MyProject', '[/\\]')

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.