See ECMA 262 v3 §15.5.4.14, pp.103,104 for details. The rules are complex and might seem almost arbitrary without close examination.
Code:
''.split()
''.split(/./)
''.split(/.?/)
''.split(/.??/)
'ab'.split(/a*/)
'ab'.split(/a*?/)
'ab'.split(/(?:ab)/)
'ab'.split(/(?:ab)*/)
'ab'.split(/(?:ab)*?/)
'test'.split('')
'test'.split()
'111'.split(1)
'test'.split(/(?:)/, 2)
'test'.split(/(?:)/, -1)
'test'.split(/(?:)/, undefined)
'test'.split(/(?:)/, null)
'test'.split(/(?:)/, NaN)
'test'.split(/(?:)/, true)
'test'.split(/(?:)/, '2')
'test'.split(/(?:)/, 'two')
'a'.split(/-/)
'a'.split(/-?/)
'a'.split(/-??/)
'a'.split(/a/)
'a'.split(/a?/)
'a'.split(/a??/)
'ab'.split(/-/)
'ab'.split(/-?/)
'ab'.split(/-??/)
'a-b'.split(/-/)
'a-b'.split(/-?/)
'a-b'.split(/-??/)
'a--b'.split(/-/)
'a--b'.split(/-?/)
'a--b'.split(/-??/)
''.split(/()()/)
'.'.split(/()()/)
'.'.split(/(.?)(.?)/)
'.'.split(/(.??)(.??)/)
'.'.split(/(.)?(.)?/)
'A<B>bold</B>and<CODE>coded</CODE>'.split(ecmaSampleRegex)
'test'.split(/(.?)/)
'tesst'.split(/(s)*/)
'tesst'.split(/(s)*?/)
'tesst'.split(/(s*)/)
'tesst'.split(/(s*?)/)
'tesst'.split(/(?:s)*/)
'tesst'.split(/(?=s+)/)
'test'.split('t')
'test'.split('es')
'test'.split(/t/)
'test'.split(/es/)
'test'.split(/(t)/)
'test'.split(/(es)/)
'test'.split(/(t)(e)(s)(t)/)
'.'.split(/(((.((.??)))))/)
'.'.split(/(((((.??)))))/)
'a b c d'.split(/ /, -(Math.pow(2, 32) - 1))
'a b c d'.split(/ /, Math.pow(2, 32) + 1)
'a b c d'.split(/ /, Infinity)
Expected:
[""]
[""]
[]
[]
["", "b"]
["a", "b"]
["", ""]
["", ""]
["a", "b"]
["t", "e", "s", "t"]
["test"]
["", "", "", ""]
["t", "e"]
["t", "e", "s", "t"]
["t", "e", "s", "t"]
[]
[]
["t"]
["t", "e"]
[]
["a"]
["a"]
["a"]
["", ""]
["", ""]
["a"]
["ab"]
["a", "b"]
["a", "b"]
["a", "b"]
["a", "b"]
["a", "-", "b"]
["a", "", "b"]
["a", "", "b"]
["a", "-", "-", "b"]
[]
["."]
["", ".", "", ""]
["."]
["", ".", undefined, ""]
["A", undefined, "B", "bold", "/", "B", "and", undefined, "CODE", "coded", "/", "CODE", ""]
["", "t", "", "e", "", "s", "", "t", ""]
["t", undefined, "e", "s", "t"]
["t", undefined, "e", undefined, "s", undefined, "s", undefined, "t"]
["t", "", "e", "ss", "t"]
["t", "", "e", "", "s", "", "s", "", "t"]
["t", "e", "t"]
["te", "s", "st"]
["", "es", ""]
["t", "t"]
["", "es", ""]
["t", "t"]
["", "t", "es", "t", ""]
["t", "es", "t"]
["", "t", "e", "s", "t", ""]
["", ".", ".", ".", "", "", ""]
["."]
["a"]
["a"]
[]
Native (This Browser):
[""]
[""]
[]
[]
["", "b"]
["a", "b"]
["", ""]
["", ""]
["a", "b"]
["t", "e", "s", "t"]
["test"]
["", "", "", ""]
["t", "e"]
["t", "e", "s", "t"]
["t", "e", "s", "t"]
[]
[]
["t"]
["t", "e"]
[]
["a"]
["a"]
["a"]
["", ""]
["", ""]
["a"]
["ab"]
["a", "b"]
["a", "b"]
["a", "b"]
["a", "b"]
["a", "-", "b"]
["a", "", "b"]
["a", "", "b"]
["a", "-", "-", "b"]
[]
["."]
["", ".", "", ""]
["."]
["", ".", undefined, ""]
["A", undefined, "B", "bold", "/", "B", "and", undefined, "CODE", "coded", "/", "CODE", ""]
["", "t", "", "e", "", "s", "", "t", ""]
["t", undefined, "e", "s", "t"]
["t", undefined, "e", undefined, "s", undefined, "s", undefined, "t"]
["t", "", "e", "ss", "t"]
["t", "", "e", "", "s", "", "s", "", "t"]
["t", "e", "t"]
["te", "s", "st"]
["", "es", ""]
["t", "t"]
["", "es", ""]
["t", "t"]
["", "t", "es", "t", ""]
["t", "es", "t"]
["", "t", "e", "s", "t", ""]
["", ".", ".", ".", "", "", ""]
["."]
["a"]
["a"]
[]
Fixed:
[""]
[""]
[]
[]
["", "b"]
["a", "b"]
["", ""]
["", ""]
["a", "b"]
["t", "e", "s", "t"]
["test"]
["", "", "", ""]
["t", "e"]
["t", "e", "s", "t"]
["t", "e", "s", "t"]
[]
[]
["t"]
["t", "e"]
[]
["a"]
["a"]
["a"]
["", ""]
["", ""]
["a"]
["ab"]
["a", "b"]
["a", "b"]
["a", "b"]
["a", "b"]
["a", "-", "b"]
["a", "", "b"]
["a", "", "b"]
["a", "-", "-", "b"]
[]
["."]
["", ".", "", ""]
["."]
["", ".", undefined, ""]
["A", undefined, "B", "bold", "/", "B", "and", undefined, "CODE", "coded", "/", "CODE", ""]
["", "t", "", "e", "", "s", "", "t", ""]
["t", undefined, "e", "s", "t"]
["t", undefined, "e", undefined, "s", undefined, "s", undefined, "t"]
["t", "", "e", "ss", "t"]
["t", "", "e", "", "s", "", "s", "", "t"]
["t", "e", "t"]
["te", "s", "st"]
["", "es", ""]
["t", "t"]
["", "es", ""]
["t", "t"]
["", "t", "es", "t", ""]
["t", "es", "t"]
["", "t", "e", "s", "t", ""]
["", ".", ".", ".", "", "", ""]
["."]
["a"]
["a"]
[]
« XRegExp © 2008-2012 Steven Levithan. These tests are released under the MIT License.