简体   繁体   中英

How to remove the last word in a string using 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?

var str = "I want to remove the last word.";
var lastIndex = str.lastIndexOf(" ");

str = str.substring(0, lastIndex);

Get last space and then get sub string.

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.

只是为了好玩而胡闹,这是一种有趣而令人发指的方式!

"I want to remove the last word.".split(" ").reverse().slice(1).reverse().join(" ")

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.

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

如果这里的其他人试图将字符串名称拆分为姓氏和名字,请确保处理名称只有单词的情况。

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"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM