adsds
adsds

Reputation: 123

how to take whitespace and newline in split function

I must iterate in a string like this:

message:    dob cat dog dog
            cat cat

Like a multiple separate I need white space and newline (\n) so my function is:

message.split(' ,\n').forEach(function(x){
                ...});

But this is not work. Anyone can help me?

Upvotes: 0

Views: 61

Answers (2)

Rajeesh Menoth
Rajeesh Menoth

Reputation: 1750

You can use this also.

message.split(/\s/)

Demo : Click here

Upvotes: 0

Piotr Łużecki
Piotr Łużecki

Reputation: 1041

I would rather use regex:

message.split(/[\ \n]/)

[] - matches one of characters in this group,

'\ ' - will match single space,

'\n' - will match new line

Upvotes: 2

Related Questions