5

I want to split string on every third space. For example :

var str = 'Lorem ipsum dolor sit amet consectetur adipiscing elit sed do eiusmod tempor incididunt';
//result will be:
var result = ["Lorem ipsum dolor ", "sit amet consectetur ", "adipiscing elit sed ", "do eiusmod tempor ", "incididunt"];

Please help me. Thank you

Spella
  • 139
  • 1
  • 10
  • I already use that function, it's different. I want to split every third space not at certain position. – Spella Jul 10 '17 at 06:33

4 Answers4

11

Use regex for splitting the string.

var str = 'Lorem ipsum dolor sit amet consectetur adipiscing elit sed do eiusmod tempor incididunt';
var splited = str.match(/\b[\w']+(?:[^\w\n]+[\w']+){0,2}\b/g);
console.log(splited);

Regex description:

 1. \b assert position at a word boundary (^\w|\w$|\W\w|\w\W)

 2. Match a single character present in the list below [\w']+
         \w matches any word character (equal to [a-zA-Z0-9_])

 3. {0,2} Quantifier — Matches between 0 and 2 times

 4. Match a single character not present in the list below [^\w\n]

 5. \w matches any word character (equal to [a-zA-Z0-9_])

 6. \n matches a line-feed (newline) character (ASCII 10)

 7. Match a single character present in the list below [\w']

 8. \w matches any word character (equal to [a-zA-Z0-9_])

 9. ' matches the character ' literally (case sensitive)

 10. \b assert position at a word boundary (^\w|\w$|\W\w|\w\W)

 11. g modifier: global. All matches (don't return after first match)
Clonkex
  • 3,373
  • 7
  • 38
  • 55
brk
  • 48,835
  • 10
  • 56
  • 78
  • 1
    kindly add the explanation for the regex :) – guradio Jul 10 '17 at 06:29
  • Wow what a regex.. Thank you for your answer and explanation :) I still need to learn how to use regex. – Spella Jul 10 '17 at 06:35
  • 1
    @brk Might want to add `` before the regex explanation codebox so that it doesn't colour it weirdly :) – Clonkex Jul 10 '17 at 06:35
  • 1
    @Spella It's easier than you think. Just go for it! [This SO answer](https://stackoverflow.com/a/2759417/2288578) is by far the best regex tutorial I found and it's how I learned. – Clonkex Jul 10 '17 at 06:38
  • Thank you very much.. I will follow this tutorial :) – Spella Jul 10 '17 at 06:40
  • I found that this regex pattern will emit the `(` and `)` characters in some cases, e.g. `This is an example (2020)` will emit the last `)` – Laurynas Jan 09 '20 at 13:08
4

var str = 'Lorem ipsum dolor sit amet consectetur adipiscing elit sed do eiusmod tempor incididunt';
var splitString = str.match(/(.*?\s){3}/g);
console.log(splitString);
Dij
  • 9,761
  • 4
  • 18
  • 35
2

check here https://regex101.com/r/npQt7X/1

or

const regex = /\b[\w']+(?:[^\w\n]+[\w']+){0,2}\b/g;
const str = `Lorem ipsum dolor sit amet consectetur adipiscing elit sed do eiusmod tempor incididunt`;
let m;

while ((m = regex.exec(str)) !== null) {
    // This is necessary to avoid infinite loops with zero-width matches
    if (m.index === regex.lastIndex) {
        regex.lastIndex++;
    }

    // The result can be accessed through the `m`-variable.
    m.forEach((match, groupIndex) => {
        console.log(`Found match, group ${groupIndex}: ${match}`);
    });
}
Masum billah
  • 982
  • 7
  • 24
2

Regexes are nice, but hard to explain and difficult to read.

So I use regexes only if there is no other solution.

An argument against these regexes is the hardcoding of the parameters.

var str = 'Lorem ipsum dolor sit amet consectetur adipiscing elit sed do eiusmod tempor incididunt';

var splitMyString = (str, splitLength) => {
  var a = str.split(' '), b = [];
  while(a.length) b.push(a.splice(0,splitLength).join(' '));
  return b;
}
console.log(splitMyString(str,3));
Frank Wisniewski
  • 1,182
  • 8
  • 7