Rob
0
Q:

javascript get last n characters of string

var hello = "Hello World";
var lastCharOfHello=hello.slice(-1);//d
11
var str = "Hello";
var newString = str.substring(0, str.length - 1); //newString = Hell
14
const id = "ctl03_Tabs1";
console.log(id.slice(id.length - 5)); //Outputs: Tabs1
console.log(id.slice(id.length - 1)); //Outputs: 1
3
'abc'.slice(-1); // c
6
// 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];
}
1

New to Communities?

Join the community