繁体   English   中英

如何使用jQuery获取句子的最后一个单词?

[英]How to get the last word of a sentence using jQuery?

如何使用jQuery获取句子的最后一个单词?

例子:

  • get the last word of a sentence应该返回sentence
  • get the last word应该返回word

试试这个

var lastword = str.split(" ").pop();

它可以使用JavaScript完成。 为什么选择JQuery?

这将split的字符串转换成单个组件,然后它将会弹出数组的最后一个元素并将其返回。

更多信息在这里

 alert("get the last word of a sentence".split(" ").pop()); 

试试这个。

var arr = str.split(' ');

var length = arr.length;

var result = arr[length - 1];

return result;

或者你也可以尝试一下。

var lastitem = str.split(" ").pop();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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