1

我有一个这样的网站结构:

/docs/one_of_the_resources/one-of-the-resources.html
/docs/a_complete_different_resource/a-complete-different-resource.html

我想摆脱 url 中的所有子文件夹并得到这个:

/one-of-the-resources.html
/a-complete-different-resource.html

子文件夹不应受到影响:

/docs/one_of_the_resources/assets/*

文件夹名称始终与 html 文件相同,只是破折号与下划线交换,当然没有后缀。

我正在使用grunt-contrib-rewrite和 grunt-connect。

不能把我的头绕过去。这甚至可能吗?

4

1 回答 1

2

您可以使用否定字符类

/\/[^/]+$/
  • [^/]+匹配除 a 以外的任何内容/。量词+确保一个或多个字符。

  • $将正则表达式锚定在字符串的末尾。

正则表达式演示

例子

string = "/docs/one_of_the_resources/one-of-the-resources.html";
console.log(string.match(/\/[^/]+$/)[0]);
// => one-of-the-resources.html
于 2015-06-26T11:18:11.407 回答