tags:

views:

46

answers:

1

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

+2  A: 

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(/ +/);
Tomalak
I'm still getting "Uncaught SyntaxError: Unexpected token ILLEGAL" in Chrome.
fuSi0N
@fuSi0N: That's a syntax error somewhere else and has nothing to do with splitting a string.
Tomalak
Definitely has something to do with split(). As soon as i remove the first split(), it throws the same error at line: 83. Any ideas?
fuSi0N
@fuSi0N: You have a broken UTF-8 character in your .js file right between the `Date.split(/ +/)` and the `;`. If you are interested, open the .js file in a hex editor and see for yourself. To fix it, just delete the entire line and rewrite it from scratch, instead of copy/pasting it from the clipboard all the time.
Tomalak