How to remove the last word in a string using JavaScript
JavascriptJavascript Problem Overview
How can I remove the last word in the string using JavaScript?
For example, the string is "I want to remove the last word."
After using removal, the string in the textbox will display "I want to remove the last"
I've seen how to remove the last character using the substring function, but because the last word can be different every time. Is there a way to count how many words are required to remove in JavaScript?
Javascript Solutions
Solution 1 - Javascript
Use:
var str = "I want to remove the last word.";
var lastIndex = str.lastIndexOf(" ");
str = str.substring(0, lastIndex);
Get the last space and then get the substring.
Solution 2 - Javascript
An easy way to do that would be to use JavaScript's lastIndexOf() and substr() methods:
var myString = "I want to remove the last word";
myString = myString.substring(0, myString.lastIndexOf(" "));
Solution 3 - Javascript
You can do a simple regular expression like so:
"I want to remove the last word.".replace(/\w+[.!?]?$/, '')
>>> "I want to remove the last"
Finding the last index for " "
is probably faster though. This is just less code.
Solution 4 - Javascript
Following answer by Amir Raminfar, I found this solution. In my opinion, it's better than accepted answer, because it works even if you have a space at the end of the string or with languages (like French) that have spaces between last word and punctuation mark.
"Je veux supprimer le dernier mot !".replace(/[\W]*\S+[\W]*$/, '')
"Je veux supprimer le dernier"
It strips also the space(s) and punctuation marks before the last word, as the OP implicitly required.
Peace.
Solution 5 - Javascript
Fooling around just for fun, this is a funny and outrageous way to do it!
"I want to remove the last word.".split(" ").reverse().slice(1).reverse().join(" ")
Solution 6 - Javascript
Use the split
function:
var myString = "I want to remove the last word";
var mySplitResult = myString.split(" ");
var lastWord = mySplitResult[mySplitResult.length-1]
Solution 7 - Javascript
The shortest answer to this question would be as below,
var str="I want to remove the last word".split(' ');
var lastword=str.pop();
console.log(str.join(' '));
Solution 8 - Javascript
You can match the last word following a space that has no word characters following it.
word=/s+\W*([a-zA-Z']+)\W*$/.exec(string);
if(word) alert(word[1])
Solution 9 - Javascript
If anyone else here is trying to split a string name in to last name and first name, please make sure to handle the case in which the name has only word.
let recipientName = _.get(response, 'shipping_address.recipient_name'); let lastWordIndex = recipientName.lastIndexOf(" "); let firstName = (lastWordIndex == -1) ? recipientName : recipientName.substring(0, lastWordIndex); let lastName = (lastWordIndex == -1) ? '' : recipientName.substring(lastWordIndex + 1);
Solution 10 - Javascript
To get the last word
const animals = "I want to remove the last word.".split(" ");
console.log(animals.slice(-1));
Expected output: word.
To remove the last word
console.log(animals.slice(0, -1).join(" ");
Expected output: "I want to remove the last"