我想使用 Google Apps 脚本将包含正向和/或反向斜杠的路径拆分为多个部分。 我更喜欢使用单个 RegExp 来完成此操作,而不是编写大量代码。
所以代码应该看起来像
var myPath = "<something>"; // i.e. \first\second/third\filename.extension
var parts = myPath.replace(<a regular expression>).split('/');
尽管我进行了搜索,但没有找到解决方案。
应该做什么<a regular expression>
是?
最佳答案
如果您只是要拆分,则无需事先更换然后拆分。您可以将正则表达式直接传递给 split
函数。像这样:
var parts = mPath.split(/[/\\]/);
关于javascript - 使用包含\和/使用正则表达式分割路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23683130/