String split returns an array with two elements instead of one

后端 未结 12 1571
渐次进展
渐次进展 2020-12-03 06:15

I don\'t understand this behaviour:

var string = \'a,b,c,d,e:10.\';
var array = string.split (\'.\');

I expect this:

consol         


        
12条回答
  •  情书的邮戳
    2020-12-03 07:17

    According to MDN web docs:

    Note: When the string is empty, split() returns an array containing one empty string, rather than an empty array. If the string and separator are both empty strings, an empty array is returned.

    const myString = '';
    const splits = myString.split();
    
    console.log(splits); 
    
    // ↪ [""]
    

提交回复
热议问题