Here is the example:
// [JS] Capture last 2 characters with: textRange or contents
// https://community.adobe.com/t5/illustrator-discussions/js-capture-last-2-characters-with-textrange-or-contents/m-p/12390176#M291706
// catch the last two characters with textRange
// select a textFrame before running this snippet
// regards pixxxelschubser
var howMany = 2; // here you can change the desired number of last characters
var chars = app.selection[0].textRange.characters;
var lastChars = chars[chars.length-howMany];
lastChars.length = howMany; // set the new textRange.length
alert(howMany+" last character(s): "+lastChars.contents);
But I prefer a different method:
// select a textFrame before running this snippet
var searchString = /(..)$/;
var aTF = app.selection[0];
var foundString = aTF.contents.match (searchString);
alert(foundString[0]);
If that works for you
have fun
😉