your code is good. but not with a split. split will consider it as a limiter. for example, something like this:
var arr = "1, 1, 1, 1"; arr.split(',') === [1, 1, 1, 1] ; //but arr.split(1) === [', ', ', ', ', ', ', '];
Use match
or exec
instead. 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);
source share