将字符串拆分为n个单词的数组

时间:2022-01-22 05:40:32

I'm trying to turn this:

我试图改变这个:

"This is a test this is a test"

into this:

进入这个:

["This is a", "test this is", "a test"]

I tried this:

我试过这个:

const re = /\b[\w']+(?:[^\w\n]+[\w']+){0,2}\b/
const wordList = sample.split(re)
console.log(wordList)

But I got this:

但我得到了这个:

[ '',
  ' ',
  ' ']

Why is this?

为什么是这样?

(The rule is to split the string every N words.)

(规则是每N个字分割字符串。)

5 个解决方案

#1


9  

The String#split method will split the string by the matched content so it won't include the matched string within the result array.

String#split方法将按匹配的内容拆分字符串,因此它不会在结果数组中包含匹配的字符串。

Use the String#match method with a global flag (g) on your regular expression instead:

使用String#match方法在正则表达式上使用全局标志(g):

var sample="This is a test this is a test"

const re = /\b[\w']+(?:\s+[\w']+){0,2}/g;
const wordList = sample.match(re);
console.log(wordList);

Regex explanation here.

正则表达式在这里解释。

#2


4  

your code is good to go. but not with split. split will treat it as a delimitor. for instance something like this:

你的代码很好用。但不是分裂。拆分将其视为一个分隔符。例如这样的事情:

var arr = "1, 1, 1, 1";
arr.split(',') === [1, 1, 1, 1] ;
//but 
arr.split(1) === [', ', ', ', ', ', ', '];

Instead use match or exec. like this

而是使用匹配或执行。喜欢这个

var x = "This is a test this is a test";
var re = /\b[\w']+(?:[^\w\n]+[\w']+){0,2}\b/g
var y = x.match(re);
console.log(y);

#3


2  

As an alternate approach, you can split string by space and the merge chunks in batch.

作为替代方法,您可以按空格分割字符串,并批量分割合并块。

function splitByWordCount(str, count) {
  var arr = str.split(' ')
  var r = [];
  while (arr.length) {
    r.push(arr.splice(0, count).join(' '))
  }
  return r;
}

var a = "This is a test this is a test";
console.log(splitByWordCount(a, 3))
console.log(splitByWordCount(a, 2))

#4


2  

You could split like that:

你可以这样分开:

var str = 'This is a test this is a test';
var wrd = str.split(/((?:\w+\s+){1,3})/);
console.log(wrd);

But, you have to delete empty elements from the array.

但是,您必须从数组中删除空元素。

#5


0  

Use whitespace special character (\s) and match function instead of split:

使用空格特殊字符(\ s)和匹配函数而不是拆分:

var wordList = sample.text().match(/\s?(?:\w+\s?){1,3}/g);

Split breaks string where regex matches. Match returns whatever that is matched.

拆分正则表达式匹配的字符串。匹配返回匹配的任何内容。

Check this fiddle.

检查这个小提琴。

#1


9  

The String#split method will split the string by the matched content so it won't include the matched string within the result array.

String#split方法将按匹配的内容拆分字符串,因此它不会在结果数组中包含匹配的字符串。

Use the String#match method with a global flag (g) on your regular expression instead:

使用String#match方法在正则表达式上使用全局标志(g):

var sample="This is a test this is a test"

const re = /\b[\w']+(?:\s+[\w']+){0,2}/g;
const wordList = sample.match(re);
console.log(wordList);

Regex explanation here.

正则表达式在这里解释。

#2


4  

your code is good to go. but not with split. split will treat it as a delimitor. for instance something like this:

你的代码很好用。但不是分裂。拆分将其视为一个分隔符。例如这样的事情:

var arr = "1, 1, 1, 1";
arr.split(',') === [1, 1, 1, 1] ;
//but 
arr.split(1) === [', ', ', ', ', ', ', '];

Instead use match or exec. like this

而是使用匹配或执行。喜欢这个

var x = "This is a test this is a test";
var re = /\b[\w']+(?:[^\w\n]+[\w']+){0,2}\b/g
var y = x.match(re);
console.log(y);

#3


2  

As an alternate approach, you can split string by space and the merge chunks in batch.

作为替代方法,您可以按空格分割字符串,并批量分割合并块。

function splitByWordCount(str, count) {
  var arr = str.split(' ')
  var r = [];
  while (arr.length) {
    r.push(arr.splice(0, count).join(' '))
  }
  return r;
}

var a = "This is a test this is a test";
console.log(splitByWordCount(a, 3))
console.log(splitByWordCount(a, 2))

#4


2  

You could split like that:

你可以这样分开:

var str = 'This is a test this is a test';
var wrd = str.split(/((?:\w+\s+){1,3})/);
console.log(wrd);

But, you have to delete empty elements from the array.

但是,您必须从数组中删除空元素。

#5


0  

Use whitespace special character (\s) and match function instead of split:

使用空格特殊字符(\ s)和匹配函数而不是拆分:

var wordList = sample.text().match(/\s?(?:\w+\s?){1,3}/g);

Split breaks string where regex matches. Match returns whatever that is matched.

拆分正则表达式匹配的字符串。匹配返回匹配的任何内容。

Check this fiddle.

检查这个小提琴。