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?

有帮助吗?

解决方案

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(/[/\\]/);
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top