tsiokos
tsiokos

Reputation: 1090

How to split spaces in strings (cross-browser safe)

What is the best cross-browser practice to split spaces in a string with Javascript? I tried theString.split(" ") but i am having issues with IE and Chrome/Safari.

Update

Here's the js code. IE/Chrome throw error at line: 61

http://pastie.org/1151951

Upvotes: 0

Views: 1077

Answers (2)

Balaji Gunasekaran
Balaji Gunasekaran

Reputation: 435

Below seems to work fine for me

someText.split(/\s/)

With whitespace removal

someText.split(/\s+/)

Upvotes: 0

Tomalak
Tomalak

Reputation: 338218

To avoid having empty parts, split with the help of a regex:

var parts = theString.split(/ +/);

Optionally, trim string before splitting.

var parts = theString.replace(/^\s+|\s+$/g, "").split(/ +/);

Upvotes: 2

Related Questions