javascript jQuery 返回不带逗号的字符串的前 5 个单词

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13146613/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 17:56:24  来源:igfitidea点击:

jQuery return first 5 words of a string without commas

javascriptjqueryarraysstring

提问by ccnokes

I'm trying to return the first 5 words of a string in a readable format, no "" or commas separating words. I'm not sure if its a regex thing or what, but I can't figure it out although its probably simple. Thanks!

我正在尝试以可读格式返回字符串的前 5 个单词,没有“”或逗号分隔单词。我不确定它是正则表达式还是什么,但我无法弄清楚,尽管它可能很简单。谢谢!

See what I have thus far: http://jsfiddle.net/ccnokes/GktTd/

看看我到目前为止所拥有的:http: //jsfiddle.net/ccnokes/GktTd/

This is the function I'm using:

这是我正在使用的功能:

function getWords(string){
    var words = string.split(/\s+/).slice(1,5);
    return words;
}

回答by Troy Alford

The only thing you are missing is a join()

你唯一缺少的是一个 join()

Try this:

试试这个:

function getWords(str) {
    return str.split(/\s+/).slice(0,5).join(" ");
}

This will do something like:

这将执行以下操作:

var str = "This is a long string with more than 5 words.";
console.log(getWords(str)); // << outputs "This is a long string"

Take a look at this linkfor a further explanation of the .join(). function in javascript. Essentially - if you don't supply an argument, it uses the default delimiter ,, whereas if you supply one (as I'm doing in the above example, by providing " "- it will use that instead. This is why the output becomes the first 5 words, separated by a space between each.

请查看此链接以进一步解释.join(). javascript中的函数。本质上 - 如果你不提供参数,它使用默认分隔符,,而如果你提供一个(正如我在上面的例子中所做的那样,通过提供" "- 它会使用它来代替。这就是为什么输出成为第一个5 个单词,每个单词之间用空格隔开。

回答by Eric

Those commas are coming from how you output the data:

这些逗号来自您输出数据的方式:

//write to DOM
$('<h2 />').text(getWords(str).join(' ')).appendTo('body');

? When you add a string to getWords(str), javascript tries to convert the array into a string - it does this by joining the words with commas. If you want to join them with something else, use join.

? 当您向 中添加字符串时getWords(str),javascript 会尝试将数组转换为字符串 - 它通过用逗号连接单词来实现。如果您想将它们与其他东西一起使用,请使用join.

回答by Krzysztof Grzybek

Troy Alford solution is working, but is has one drawback. If between words there will be more than one space, or new line character (\n) it will be converted to single space, e.g:

Troy Alford 解决方案有效,但有一个缺点。如果单词之间有多个空格,或者换行符 (\n) 将被转换为单个空格,例如:

'jQuery is a
multi-browser'

'jQuery 是一个
多浏览器'

will be converted to

将被转换为

'jQuery is a multi-browser'

'jQuery 是一个多浏览器'

To fix this issue, we might use word boundary regex. It might looks like this:

为了解决这个问题,我们可能会使用词边界正则表达式。它可能看起来像这样:

function getFirstWords(text, wordsAmount) {
  const arr = text.split(/\b/g);
  const arrItemsAmount = (/\b/.exec(text) && /\b/.exec(text).index) ?
                           wordsAmount * 2 :
                           wordsAmount * 2 - 1;
  return arr.slice(0, arrItemsAmount).join('');
}