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

2
  • why is no space around 'i'? Commented Jan 10, 2018 at 12:48
  • I am just showing you example thanks alot for helping me Commented Jan 10, 2018 at 12:52

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

Comments

1

You can use regex in split.

var a =`                     that i love         
           game1           `;
console.log(a.split(/(\s+\S+\s+)/));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.