UnKnown UnKnown - 1 month ago 20
Javascript Question

How to get last word name from a string

I have a word such as

var text = 'http://xxx:9696/images/FSDefault.jpg';


How to get only the word FSDefault.jpg

Answer

Use String#split method to split based on delimiter / and get the last element of the array using Array#pop method.

var text = 'http://xxx:9696/images/FSDefault.jpg';

console.log(text.split('/').pop())


Or you can use String#lastIndexOf method along with String#substr method.

var text = 'http://xxx:9696/images/FSDefault.jpg';

console.log(text.substr(text.lastIndexOf('/') + 1))