如何从字符串中删除前100个单词?

Mal*_*001 5 javascript

我只想删除前100个单词并保留字符串中的剩余部分.

我在下面的代码完全相反:

   var short_description = description.split(' ').slice(0,100).join(' ');
Run Code Online (Sandbox Code Playgroud)

Doo*_*nob 17

删除第一个参数:

var short_description = description.split(' ').slice(100).join(' ');
Run Code Online (Sandbox Code Playgroud)

使用slice(x, y)会给你从元素xy,但使用slice(x)会给你从元素x到数组的末尾.(注意:如果描述少于100个单词,这将返回空字符串.)

这是一些文档.

你也可以使用正则表达式:

var short_description = description.replace(/^([^ ]+ ){100}/, '');
Run Code Online (Sandbox Code Playgroud)

以下是正则表达式的解释:

^      beginning of string
(      start a group
[^ ]   any character that is not a space
+      one or more times
       then a space
)      end the group. now the group contains a word and a space.
{100}  100 times
Run Code Online (Sandbox Code Playgroud)

然后用零替换那100个单词.(注意:如果描述少于100个单词,这个正则表达式将只返回描述不变.)