开发者

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

开发者 https://www.devze.com 2023-01-15 23:32 出处:网络
What is the best开发者_Go百科 cross-browser practice to split spaces in a string with Javascript?

What is the best开发者_Go百科 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


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(/ +/);


Below seems to work fine for me

someText.split(/\s/)

With whitespace removal

someText.split(/\s+/)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号