拆分空白帮助

时间:2011-04-01 02:10:43

标签: javascript regex

// alerts -> a,b,c,d
// which is correct
window.alert("a b c d".split(/[\u000A-\u000D\u2028\u2029\u0009\u0020\u00A0\uFEFF]+/g));

但是,这不是:

// alerts -> ,a,b,c,d,
// which is not what we need
// as you can see, there is an extra space
// in the front and end of the array
// it is supposed to alert -> a,b,c,d
// just like our first example

window.alert("   a b c d   ".split(/[\u000A-\u000D\u2028\u2029\u0009\u0020\u00A0\uFEFF]+/g));

任何想法的人?

1 个答案:

答案 0 :(得分:3)

您始终可以匹配该组的反转而不是分割。

"   a b c d   ".match(/[^\u000A-\u000D\u2028\u2029\u0009\u0020\u00A0\uFEFF]+/g)

生成["a", "b", "c", "d"]