Sunday 9 December 2018

Javascript .split behaviour

Javascript .split behaviour


var string = 'a,b,c,d,e:10.';
var array = string.split ('.').filter(function(el) {return el.length != 0});
 


This is the correct and expected behavior. Given that you've included the separator in the string, the split function (simplified) takes the part to the left of the separator ("a,b,c,d,e:10") as the first element and the part to the rest of the separator (an empty string) as the second element. If you're really curious about how split() works, you can check out pages 148 and 149 of the ECMA spec (ECMA 262)
Sources
Stack Overflow

No comments:

Post a Comment