68

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?

10 Answers 10

149

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.

Sign up to request clarification or add additional context in comments.

2 Comments

This is a great solution. It works for me, and I'm going to enhance it.
It only works with string without line breaks. Otherwise choose @abePdIta solution.
24

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(" "));

Comments

11

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.

Comments

8

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.

Comments

4

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(" ")

Comments

3

Use the split function:

var myString = "I want to remove the last word";
var mySplitResult = myString.split(" ");
var lastWord =  mySplitResult[mySplitResult.length-1]

2 Comments

This one is also work for me to retrieve the last word in the string. Thanks
Doesn't fulfill the requirements in the question.
2

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(' '));

2 Comments

Your string is too long for the example :P
Just a pity that it does not answer the question.
1

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])

Comments

0

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);

Comments

0

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"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.