Question

I want to split a path containing forward and/or backward slashes into parts using Google Apps script. I prefer doing this by using a single RegExp instead of writing much code.

so code should look like

var myPath  = "<something>";        // i.e.   \first\second/third\filename.extension
var parts = myPath.replace(<a regular expression>).split('/');

Allthough I searched SO, I didn't find a solution.

What should <a regular expression> be?

Was it helpful?

Solution

If you're just going to split, there's no need to replace beforehand then split. You can pass the regex to the split function directly. Like this:

var parts = mPath.split(/[/\\]/);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top