EDIT: As others have pointed out, use slice(-5) instead of substr. However, see the .split().pop() solution at the bottom of this answer for another approach.

Original answer:

You'll want to use the Javascript string method .substr() combined with the .length property.

var id = "ctl03_Tabs1";
var lastFive = id.substr(id.length - 5); // => "Tabs1"
var lastChar = id.substr(id.length - 1); // => "1"

This gets the characters starting at id.length - 5 and, since the second argument for .substr() is omitted, continues to the end of the string.

You can also use the .slice() method as others have pointed out below.

If you're simply looking to find the characters after the underscore, you could use this:

var tabId = id.split("_").pop(); // => "Tabs1"

This splits the string into an array on the underscore and then "pops" the last element off the array (which is the string you want).

Answer from Jamon Holmgren on Stack Overflow
Top answer
1 of 16
1429

EDIT: As others have pointed out, use slice(-5) instead of substr. However, see the .split().pop() solution at the bottom of this answer for another approach.

Original answer:

You'll want to use the Javascript string method .substr() combined with the .length property.

var id = "ctl03_Tabs1";
var lastFive = id.substr(id.length - 5); // => "Tabs1"
var lastChar = id.substr(id.length - 1); // => "1"

This gets the characters starting at id.length - 5 and, since the second argument for .substr() is omitted, continues to the end of the string.

You can also use the .slice() method as others have pointed out below.

If you're simply looking to find the characters after the underscore, you could use this:

var tabId = id.split("_").pop(); // => "Tabs1"

This splits the string into an array on the underscore and then "pops" the last element off the array (which is the string you want).

2 of 16
1084

Don't use the deprecated .substr()!

Use either the .slice() method because it is cross browser compatible (see issue with IE). Or use the .substring() method.

The are slight differences in requirements, which are properly documented on: String.prototype.substring()

const id = "ctl03_Tabs1";

console.log(id.slice(-5)); //Outputs: Tabs1
console.log(id.slice(-1)); //Outputs: 1

// below is slower
console.log(id.substring(id.length - 5)); //Outputs: Tabs1
console.log(id.substring(id.length - 1)); //Outputs: 1
Run code snippetEdit code snippet Hide Results Copy to answer Expand

๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ javascript โ€บ how-to-get-the-last-n-characters-of-a-string-in-javascript
JavaScript - How to Get the Last N Characters of a String? - GeeksforGeeks
July 23, 2025 - The slice() method is the most commonly used approach for extracting the last N characters, thanks to its simplicity and support for negative indices. ... The substring() method calculates the starting index based on the string's length.
Discussions

javascript - How to select last two characters of a string - Stack Overflow
The number of characters to extract. ... Warning: Although String.prototype.substr(โ€ฆ) is not strictly deprecated (as in "removed from the Web standards"), it is considered a legacy function and should be avoided when possible. It is not part of the core JavaScript language and may be removed ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
Any ways to return the last X characters of a string?
Hey, long time lurker, first time poster so be kind ^_^. Really enjoying playing more and more with n8n. I am working on a process where I am building out a chatbot with AI. The string will increase over the conversation with the questions and answers being included and we are capped by the ... More on community.n8n.io
๐ŸŒ community.n8n.io
3
0
March 31, 2022
How do I get the last two characters in a string?
Well, "character" isn't well defined in unicode, so it depends. But String::pop() returns a char, which is a unicode scalar value, so we'll go with that :) Easiest way is to use an iterator: let s = String::from("hello world"); let last_two: Vec = s.chars().rev().take(2).collect(); println!("{:?}", last_two); Take the chars, reverse it, then take two. More on reddit.com
๐ŸŒ r/rust
15
8
November 16, 2015
Replace last character in string
Just depends how complex you want to make it. You could technically do it all in the pipeline: 'East1','West2' | ForEach-Object { if ($_[-1] -eq '1') { ($_.substring(0,$_.length-1) + '2') } else { ($_.substring(0,$_.length-1) + '1') } } East2 West1 More on reddit.com
๐ŸŒ r/PowerShell
10
4
January 16, 2020
๐ŸŒ
Stack Abuse
stackabuse.com โ€บ bytes โ€บ trim-the-last-n-characters-from-a-string-in-javascript
Trim the Last N Characters from a String in JavaScript
August 21, 2023 - The String.substring() method returns a new string that starts from a specified index and ends before a second specified index. We can use this method to trim the last N characters from a string.
๐ŸŒ
MDN Web Docs
developer.mozilla.org โ€บ en-US โ€บ docs โ€บ Web โ€บ JavaScript โ€บ Reference โ€บ Global_Objects โ€บ String โ€บ substring
String.prototype.substring() - JavaScript - MDN Web Docs
The following example uses the substring() method and length property to extract the last characters of a particular string. This method may be easier to remember, given that you don't need to know the starting and ending indices as you would in the above examples.
๐ŸŒ
Techie Delight
techiedelight.com โ€บ home โ€บ java โ€บ get last n characters of a string in javascript
Get last n characters of a string in JavaScript | Techie Delight
July 7, 2026 - This post will discuss how to get the last `n` characters of a string in JavaScript... There are several methods to get the last `n` characters from a string using JavaScript native methods `substring()` and `slice()`.
๐ŸŒ
JavaScript in Plain English
javascript.plainenglish.io โ€บ javascript-get-last-n-characters-of-string-4a865e1eed7c
How to Get the Last N Characters of a String in JavaScript | JavaScript in Plain English
July 30, 2024 - In this example, we tried to get the last 50 characters of the string by passing -50 as the first argument, but the string 'Coding Beauty' contains only 13 characters. Hence, we get the entire string from slice(). We can use substring() in place of slice() to get the first N characters of a string:
๐ŸŒ
Attacomsian
attacomsian.com โ€บ blog โ€บ javascript-get-last-n-characters-of-string
How to get the last N characters of a string in JavaScript
October 25, 2022 - In the above example, we tried ... Hence, slice() returns the entire string. Alternatively, you could also use the substring() method to get the last N characters of a string in JavaScript:...
Find elsewhere
๐ŸŒ
Reactgo
reactgo.com โ€บ home โ€บ how to get last n characters of a string in javascript
How to get last n characters of a string in JavaScript | Reactgo
March 7, 2023 - To access the last n characters of a string in JavaScript, we can use the built-in slice() method by passing -n as an argument to it. -n is the number of characters we need to get from the end of a string.
๐ŸŒ
Coding Beauty
codingbeautydev.com โ€บ home โ€บ posts โ€บ how to get the last n characters of a string in javascript
How to Get the Last N Characters of a String in JavaScript - Coding Beauty
June 24, 2022 - In this example, we tried to get the last 50 characters of the string by passing -50 as the first argument, but the string 'Coding Beauty' contains only 13 characters. Hence, we get the entire string from slice(). We can use substring() in place of slice() to get the first N characters of a string:
๐ŸŒ
Bobby Hadz
bobbyhadz.com โ€บ blog โ€บ javascript-get-last-character-of-string
Get last char or last N characters of String in JavaScript | bobbyhadz
The String.substring() method doesn't support negative values to count backward, so we had to calculate the start index based on the string's length. ... The slice and substring methods are similar, however, you should use the slice method because its implementation is more intuitive. There are a couple of differences between the String.substring() and the String.slice() methods:
๐ŸŒ
Medium
medium.com โ€บ @kanmitowolabi โ€บ how-to-get-the-first-n-characters-and-last-n-characters-of-a-string-in-javascript-e9e79dc92c90
How to Get the First N Characters and Last N Characters of a String in JavaScript - Kanmit Owolabi - Medium
February 7, 2024 - const GET_FIRST_N_DIGITS = (str: any, n: number) => { return substring(0, n); } console.log(GET_FIRST_N_DIGITS("The boy is good", 6)) // The bo ยท To get the last N characters of a String, using slice(): export const GET_LAST_N_DIGITS = (str: any, n: number) => { return str.slice(str.length - n); } console.log(GET_LAST_N_DIGITS("531667272871819", 4)) // 1819 ยท Thanks! JavaScript ยท
๐ŸŒ
W3Resource
w3resource.com โ€บ javascript-exercises โ€บ javascript-basic-exercise-68.php
JavaScript basic: Create a new string taking the first and last n characters from a given string - w3resource
July 1, 2025 - // Define a function named two_string with parameters str and n function two_string(str, n) { // Extract the first part of the string from index 0 to n (exclusive) first_part = str.substring(0, n); // Extract the last part of the string from (length - n) to the end last_part = str.substring(str.length - n); // Concatenate the first and last parts and return the result return first_part + last_part; } // Call the function with sample arguments and log the results to the console console.log(two_string("JavaScript", 2)); console.log(two_string("JavaScript", 3)); ... See the Pen Create a new string taking the first and last n characters from a given string by w3resource (@w3resource) on CodePen.
๐ŸŒ
Refine
refine.dev โ€บ home โ€บ blog โ€บ tutorials โ€บ javascript substring method
JavaScript Substring Method | Refine
January 1, 2025 - While doing so, we elaborate on a few patterns of extraction from a parent string: namely, that of extracting a tail after first n characters, another of producing a substring composed of first n characters and that of extracting last n characters. In the later half of the post, we discuss some of the nuances associated with startIndex and endIndex values. In the end, we point out how JavaScript String.prototype.substring() compares to String.prototype.slice() as well as the deprecated method String.prototype.substr().
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ how-to-get-the-last-character-of-a-string-in-javascript
JavaScript โ€“ How To Get The Last Caracter of a String? | GeeksforGeeks
Here are some of the most common ... a new string from index 1 to the end.JavaScriptlet s1 = "GeeksforGeeks"; let s2 = s ... Here are the various methods to get character array from a string in JavaScript.1. Using String split() MethodThe split() Method is used to split the given string into an array of strings by separating it into substrings using a specified separator provided in the argument. JavaScriptlet s = "Geeksf ... Here are the various approaches to get the last character ...
Published: November 26, 2024
๐ŸŒ
Coderslang
learn.coderslang.com โ€บ 0179-how-to-remove-the-last-n-characters-from-a-javascript-string
How to remove the last n characters from a JavaScript string
January 24, 2022 - The slice function is a convenient way to remove the last n characters from a JavaScript string. It accepts 2 parameters. The first one is the starting point. The second one is the number of items to remove from a string.
๐ŸŒ
Tutorial Reference
tutorialreference.com โ€บ javascript โ€บ examples โ€บ faq โ€บ javascript-how-to-get-the-last-character-or-last-n-characters-of-string
How to Get the Last Character or Last N Characters of a String in JavaScript | Tutorial Reference
You may see these methods in older codebases, but they are generally not recommended for new code. ... This works, but it's verbose and less readable than at(-1). It also returns undefined for an empty string. ... This is also verbose. It returns an empty string '' for an empty string, which can sometimes be preferable to undefined. substring(): myString.substring(myString.length - 1)
๐ŸŒ
JavaScript in Plain English
javascript.plainenglish.io โ€บ javascript-get-last-two-characters-of-string-67fc4863c5b5
How to Get the Last Two Characters of a String in JavaScript | JavaScript in Plain English
July 23, 2024 - We can use substring() in place of slice() to get the first two characters of a string: const str = 'Coding Beauty';const last2 = str.substring(str.length - 2); console.log(last2); // ty ยท However, we have to manually calculate the start index ourselves with str.length - 2, which makes the code less readable. This is because unlike slice(), substring() uses 0 as the start index if a negative number is passed.
๐ŸŒ
n8n
community.n8n.io โ€บ questions
Any ways to return the last X characters of a string? - Questions - n8n Community
March 31, 2022 - Hey, long time lurker, first time poster so be kind ^_^. Really enjoying playing more and more with n8n. I am working on a process where I am building out a chatbot with AI. The string will increase over the conversation with the questions and answers being included and we are capped by the limits of prompt, what I want to be able to do is to measure the number of characters (I have found a useful post with that solution!) and if over a certain number then return the string with say the last 15...