0

I am trying to split string including string but space are also spitted.

my code:-

var a ='                     that i love          
           game1           ';
console.log(a.split(' '))

my current output is like:-

(57) ["↵", "", "", "", "", "", "", "", "", "that", "i", "love↵", "", "", "", "", "", "", "", "", "game1↵↵↵", "", "", "", ""]

Output that I am trying to get somthing like this:-

 (4)["              that",'i','               love','   ↵game'];

How can I split string in such a way including space and line break??

Please don't suggest me idea using jquery

Alisha Sharma
  • 139
  • 2
  • 15

2 Answers2

7

You can use String#match with a regular expression (regex101) to get something similar to what you want:

var a =`                     that i love          
           game1           `;

console.log(a.match(/\s*\S*/g));
 
// or

console.log(a.match(/\s*\S*\s*/g));
Ori Drori
  • 183,571
  • 29
  • 224
  • 209
1

You can use regex in split.

var a =`                     that i love         
           game1           `;
console.log(a.split(/(\s+\S+\s+)/));
Thushan
  • 1,220
  • 14
  • 14