当我尝试通过js split解析时,第一个元素是空字符串 [英] When i try to parse by js split the first element is empty string
问题描述
我尝试解析ini文件,第一个字符串是空字符串,但其他字符串没问题:
I try to parse ini file, the first string is empty string, but others okay:
结构:
[sensor1]
param1: value
[sensor2]
param1 : value
param2 : value
我的代码是:
success: function(data) {
var parsedArr = data.split(/\s*\[(.*)\]\s*\n/);
console.log(parsedArr);
}
结果:
0: ""
1: "sensor1"
2: "name: brightness temperature↵
3: "sensor2"
4: "name: brightness temp. IR↵device: HATPRO↵group:
length: 5
可以吗?以及如何解决?
Is it okay? And how to solve it?
提前致谢:)
推荐答案
要删除索引0处的空结果:
To remove the empty result at index 0:
var array = 'abcdef'.split('a');
array.shift() // Removes first element from array.
如何 split()
有效:
索引0:匹配分隔符前的所有内容
索引1:匹配给定分隔符的第一个结果
索引2:依此类推。
How split()
works:
Index 0: everything before the matching seperator
Index 1: The first result matching the given seperator
Index 2: and so on.
由于在第一场比赛之前没有任何内容,但是空字符串,数组中的第一个元素是空字符串:)。
Since there is nothing before your first match but an empty string, the first element in your array is an empty string :).
有关split()的详细文档,请查看Mozilla-Docs:
( https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/split )
For a detailed documentation about split() take a look at the Mozilla-Docs: (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/split)
这篇关于当我尝试通过js split解析时,第一个元素是空字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!