// Regex
var result = /[^/]*$/.exec("foo/bar/test.html")[0];
// Using lastIndexOf and substring
var str = "foo/bar/test.html";
var n = str.lastIndexOf('/');
var result = str.substring(n + 1);
// Using Array#split
var parts = "foo/bar/test.html".split("/");
var result = parts[parts.length - 1]; // Or parts.pop();
function test(words) {
var n = words.split(" ");
return n[n.length - 1];
}
'abc'.slice(-2);
str.charAt(str.length-1)
// strips all punctuation and returns the last word of a string
// hyphens (-) aren't stripped, add the hyphen to the regex to strip it as well
function lastWord(words) {
let n = words.replace(/[[]?.,/#!$%^&*;:{}=|_~()]/g, "").split(" ");
return n[n.length - 1];
}