Skip to content
the poor coder

how to find the last word of a string in javascript

Poor Man
Published
Reading time
2 min read
On this page

How to Find the Last Word of a String in JavaScript#

If you're working with strings in JavaScript, you might need to find the last word of a string for various reasons. Here are a few ways you can achieve this:

Method 1: Using Split and Pop#

You can split the string into an array of words using the split method and then use the pop method to get the last word of the array:

Text

const str = "This is a sample string";
const words = str.split(" ");
const lastWord = words.pop();
console.log(lastWord); // Output: "string"

Here, we first defined a string str. Then, we used the split method to split the string into an array of words using the space character as a separator. We stored this array in the words variable. Finally, we used the pop method to get the last word of the array and stored it in the lastWord variable.

Method 2: Using LastIndexOf and Substring#

You can also use the lastIndexOf method to find the index of the last space character in the string and then use the substring method to extract the last word:

Text

const str = "This is a sample string";
const lastIndex = str.lastIndexOf(" ");
const lastWord = str.substring(lastIndex + 1);
console.log(lastWord); // Output: "string"

In this method, we first defined a string str. We then used the lastIndexOf method to find the index of the last space character in the string and stored it in the lastIndex variable. Finally, we used the substring method to extract the last word from the string starting from the index after the last space character and stored it in the lastWord variable.

Method 3: Using Regular Expression#

You can also use regular expressions to find the last word of a string:

Text

const str = "This is a sample string";
const lastWord = str.match(/\w+$/)[0];
console.log(lastWord); // Output: "string"

In this method, we first defined a string str. We then used the match method to match the last word of the string using the regular expression /\w+$/, which matches any word characters (letters, digits, and underscores) at the end of the string. The match method returns an array of all matches, so we used [0] to get the first (and only) match, which is the last word of the string.

These are a few ways you can find the last word of a string in JavaScript. Choose the one that suits your need and coding style.

Keep going

2023

how to sort an array

How to Sort an Array If you have an array and you want to order its elements in a specific way, you need to use a sorting algorithm. There are several sorting algorithms available, but two of the most commonly used are…

javascript2 min read
2023

jquery mobile or desktop

jQuery Mobile or Desktop? As a web developer, I have had some experience with both jQuery Mobile and jQuery Desktop. Both frameworks have their pros and cons, and which one to use really depends on the specific project…

javascript1 min read
2023

node.js folder structure

Node.js Folder Structure If you are new to Node.js, it can be daunting to create a folder structure that will work for your project. The structure you choose will depend on the size and complexity of your project. Here…

javascript1 min read
ESC