Reputation: 965
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?
Upvotes: 68
Views: 118488
Reputation: 1
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"
Upvotes: 0
Reputation: 8101
Use the split
function:
var myString = "I want to remove the last word";
var mySplitResult = myString.split(" ");
var lastWord = mySplitResult[mySplitResult.length-1]
Upvotes: 3
Reputation: 1832
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.
Upvotes: 148
Reputation: 1899
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);
Upvotes: 0
Reputation: 1210
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(" ")
Upvotes: 4
Reputation: 123
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.
Upvotes: 8
Reputation: 2101
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(' '));
Upvotes: 2
Reputation: 12182
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(" "));
Upvotes: 24
Reputation: 34179
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.
Upvotes: 11
Reputation: 104840
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])
Upvotes: 1