jQuery返回一个没有逗号的字符串的前5个单词

时间:2022-02-14 02:37:53

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

3 个解决方案

#1


30  

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 link for 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个单词,每个单词之间用空格分隔。

#2


0  

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。

#3


0  

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

#1


30  

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 link for 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个单词,每个单词之间用空格分隔。

#2


0  

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。

#3


0  

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