String split returns an array with two elements instead of one

后端 未结 12 1572
渐次进展
渐次进展 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:01

    https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/String/split

    trim the trailing period first

    'a,b,c,d,e:10.'.replace(/\.$/g,''); // gives "a,b,c,d,e:10"
    

    then split the string

    var array = 'a,b,c,d,e:10.'.replace(/\.$/g,'').split('.');
    

    console.log (array.length); // 1

提交回复
热议问题