1. String slice() method example
This approach allows to get substring by using negative indexes.
So by using
0
and
-3
indexes as range we get
<0, text.length - 3>
text range.
var text = 'abcde';
var substring = text.slice(0, -3);
console.log(substring); // ab
2. String substring() method example
This is alternative approach to slice
method based solution.
var text = 'abcde';
var substring = text.substring(0, text.length - 3);
console.log(substring); // ab
3. String replace() method example
There is another trick that allows to replace all characters that match rexpression condition.
By using .{0,3}$
pattern we match all any 3 characters (up to 3 characters) that are loacated at the end of string.
var text = 'abcde';
var substring = text.replace(/.{0,3}$/, '');
console.log(substring); // ab