admin管理员组

文章数量:1134236

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?

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?

Share Improve this question edited Oct 30, 2021 at 19:32 Peter Mortensen 31.6k22 gold badges109 silver badges133 bronze badges asked Feb 17, 2012 at 5:05 R.SparkR.Spark 9652 gold badges16 silver badges22 bronze badges
Add a comment  | 

10 Answers 10

Reset to default 147

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.

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

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.

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.

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

Use the split function:

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

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

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

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

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"

本文标签: How to remove the last word in a string using JavaScriptStack Overflow