How can I put a string in javascript with words?

I have a large block of text, and I want to split it into an array, with each element having one line up to 50 characters long. If the string contains \ror \n, I want it to be the element itself (not part of another string). I am a little perplexed how to do this.

I tried

lines = newVal.match(/^(\r\n|.){1,50}\b/g)

I also tried to emphasize / lodash _.chunk, but it clearly ignores the words. But that only gives me the first match. Please, help.

+4
source share
4 answers

Try the following:

result = lines.split(/(?=(\r\n))/g);
result.forEach(function(item,index,arr) {
  if(item.length > 50) {
    var more = item.match(/.{1,50}\b/g);
    arr[index] = more[0];
    for(var i=1; i<more.length; ++i) {
      arr.splice(index+i, 0, more[i]);      
    }
  }
});

This will first split the text into an array with your newline characters, and then go over and continue breaking up the long lines in the array, keeping the words.

+3

String.prototype.split(), while, Array.prototype.some(), Array.prototype.filter(), String.prototype.slice(), Array.prototype.splice()

// call `.split()` of `largeBlockOfText` with `RegExp` `/\r|\n/` 
var arr = largeBlockOfText.split(/\r|\n/);
// while `arr` has element where `.length` greater than `50`
while (arr.some(function(v) {return v.length > 50})) {
  // filter element in `arr` where element has `.length` greater than `50`
  var j = arr.filter(function(val, key) {
    return val.length > 50
  })[0]
  // get index of `j` within `arr`
  , index = arr.indexOf(j)
  // text of `j`
  , text = j
  // index to insert block of 50 characters of `text`
  , i = 1;
  while (text.length) {   
    // insert block of 50 characters from `text` at `index` + 1
    arr.splice(index + i, 0, text.slice(0, 50));
    // reset `text`
    text = text.slice(50, text.length);
    // increment `i` : `index`
    ++i;
  }
  // remove `j` , reset `i`
  arr.splice(index, 1); i = 1;
}
+2

newVal.split(/\b|(?=\n)|(?=\r)/)

.reduce(function(accum,x,i){
  if(x=="\n"
  || x=="\r"
  || (accum[accum.length-1]+x).length>50
  ){
     accum.push(x);
  } else {
     accum[accum.length-1]+=x;
  }
  return accum;
 },[])

I did not handle the case of words longer than 50 characters, but this is simply because it was not explicitly clear what was going to happen

+2
source

Here is just an example of an ES6-functional approach (immutable by intent):

let _range = (count, step) => Array.apply(null, Array(count)).map((_, i) => (i * step));

let splitByLimit = function(limit = 10, string = '') {
  let length = string.length;

  if (length <= limit) {
    return [string];
  } else {
    let positions = _range(Math.ceil(length / limit), limit);

    return positions.map(pos => string.slice(pos, pos + limit));
  }
};

let getLines = function(string = '', limit = 10) {
  let splitted = string.split(/\r|\n/);
  let byLimit = splitByLimit.bind(null, limit);
  let flatten = (memo, item) => memo.concat(item);

  return splitted.map(byLimit).reduce(flatten, []);
};

live jsbin .

+1
source

Source: https://habr.com/ru/post/1620928/


All Articles