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

However, this isn't:

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

Any ideas guys?

  • Can you trim the strings first? – sdleihssirhc Apr 01 '11 at 02:14
  • Not what you're looking for, but I think using \s would help cut down on some of those hex values? – Charlie Apr 01 '11 at 02:24
  • The reason for that is because some browser's \s don't catch all of the whitespace as per the ECMAScript spec. For example, IE doesn't catch the no-break space (0x00A0). –  Apr 01 '11 at 02:32

1 Answers1

3

You can always match the inverse of that set instead of splitting.

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

produces ["a", "b", "c", "d"].

Mike Samuel
  • 118,113
  • 30
  • 216
  • 245