Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javascript: splitting a string (yet preserving the spaces) [closed]

How do I split a string like this

"please     help me "

so that I get an array like this:

["please     ","help ","me "]

In other words,I get an array which preserves the space (or spaces)

Thanks

like image 442
Zo72 Avatar asked Jul 18 '13 14:07

Zo72


People also ask

Can we divide string in JavaScript?

The split() method splits a string into an array of substrings. The split() method returns the new array. The split() method does not change the original string. If (" ") is used as separator, the string is split between words.

Which JavaScript method is used to break a string on a character or whitespace?

JavaScript String split() Method. JavaScript str. split() method is used to split the given string into an array of strings by separating it into substrings using a specified separator provided in the argument. separator: It is used to specify the character, or the regular expression, to use for splitting the string.


1 Answers

something like :

var str   = "please     help me ";
var split = str.split(/(\S+\s+)/).filter(function(n) {return n});

FIDDLE

like image 50
adeneo Avatar answered Oct 31 '22 10:10

adeneo