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

python - No exponential form of the z-axis in matplotlib-3D-plots -

php - Best Light server (Linux + Web server + Database) for Raspberry Pi -

c# - "Newtonsoft.Json.JsonSerializationException unable to find constructor to use for types" error when deserializing class -