最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Splitting a string into an array of n words - Stack Overflow

programmeradmin5浏览0评论

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.)

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.)

Share Improve this question edited Nov 26, 2016 at 10:14 alex asked Nov 26, 2016 at 10:08 alexalex 7,60115 gold badges53 silver badges79 bronze badges 3
  • What is the rule to follow to split the string? – Ajay Narain Mathur Commented Nov 26, 2016 at 10:10
  • @A.J I updated the question. – alex Commented Nov 26, 2016 at 10:11
  • 3 .split() doesn't include the delimiter so it does the opposite of what you want. You need to do a regular regex search (with a g modifier) instead of split. – JJJ Commented Nov 26, 2016 at 10:12
Add a comment  | 

5 Answers 5

Reset to default 11

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

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

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.

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))

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

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

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.

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.

发布评论

评论列表(0)

  1. 暂无评论