javascript - Trying to find the regex for pretty URLs -


i have website structure this:

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

i want rid of sub-folders in url , this:

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

sub-folders should not affected:

/docs/one_of_the_resources/assets/* 

the folder name same html file dashes swapped underline , of course there no suffix.

i'm using grunt-contrib-rewrite , grunt-connect.

can't wrap head around it. possible?

you can use negated character class

/\/[^/]+$/ 
  • [^/]+ matches other /. quantifier + ensures 1 or more characters.

  • $ anchors regex @ end of string.

regex demo

example

string = "/docs/one_of_the_resources/one-of-the-resources.html"; console.log(string.match(/\/[^/]+$/)[0]); // => one-of-the-resources.html 

Comments

Popular posts from this blog

java - HTTP Status 500 - An exception occurred processing JSP page /second.jsp at line 15 -

SQL Server - MyCTE query based on 24 hour period (next day) -

php - Using str_replace to translate a MySQL Table in html -