Home > front end >  Slice, trim the string from a specific character
Slice, trim the string from a specific character

Time:08-19

var text = ~/Desktop/Bon/ai2html-output/home-pro-Artboard_1.jpg

In js how would I get the final file name. Basically getting any string after the last '/'. It will always change.

CodePudding user response:

You can use this:

var text = '~/Desktop/Bon/ai2html-output/home-pro-Artboard_1.jpg';
var filename = text.replace(/^.*\//, ''); // 'home-pro-Artboard_1.jpg'

Explanation of regex:

  • /^.*\//: greedily remove all leading text up to and including the last slash
  • Related