截断字符串向后3级

时间:2018-04-05 21:56:42

标签: javascript

我们说我有字符串C:/Users/Me/some/random/folder/test/public/uploads/avatars\\CY4tDgB4iXfb4MTF4.png(注意双反斜杠)

如何让它成为/public/uploads/avatars/CY4tDgB4iXfb4MTF4.png?无论/avatars/[...]之后或[...]/public/之前发生什么,它都应该像那样转换它。

有什么想法吗?

1 个答案:

答案 0 :(得分:1)

您只需将\\替换为/,然后从/public/索引处获取字符串的切片:

x = "C:/Users/Me/some/random/folder/test/public/uploads/avatars\CY4tDgB4iXfb4MTF4.png"
x = x.replace('\\', '/');
return x.slice(x.indexOf('/public/'))