Regular Expression to capture after last bar

Asked

Viewed 421 times

1

I have the following URL:

("file:///home/pasta/pasta/img/intro-bg.jpg")

I wonder what regular expression I can use to capture only the intro-bg.jpg

If anyone can help me, I’d appreciate it.

  • 2

    A split() by bar does not resolve? the last element is the image.

2 answers

4

  • If you want to avoid using Submatches (or Groups), try using this expression: (?!\/)[^\/]+\/?$ that only adds a Negative Lookahead in the regular expression. The demo of the Regex101.

2

Another way:

const str = "file:///home/pasta/pasta/img/intro-bg.jpg";
const basename = str.replace(/^.*\//g, '');
console.log(basename);

  • 3

    To the user who came out distributing downvotes, please leave your comment.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.