bytes packets errs drop fifo colls carrier compressed lo: 20114 136 0 0 0 0 0 0 20114 136 0 0 0 0 0 0完整可用代码如下:exports.info = function() { // read /proc/net/dev
var fc = fs.readFileSync('/proc/net/dev','utf8'),
result = {}; // format file content
fc = fc.replace(/\s{2,}/g,' ').replace(': ',':').split("\n"); var columnLine = fc[0].split('|');
fc[0] = columnLine[columnLine.length - 1];
columnLine[columnLine.length - 1] = fc[0].substring(0, fc[0].search(/(\b\S+\:.*)/g));
fc[0] = fc[0].substring(fc[0].search(/(\b\S+\:.*)/g)); for(var i=0;i<fc.length;i++) { if(fc[i]=='') { continue; } var receiveCols = columnLine[columnLine.length - 2].split(' '),
transmitCols = columnLine[columnLine.length - 1].split(' '),
tKey = fc[i].substring(0,fc[i].indexOf(':')),
tValue = fc[i].substring(fc[i].indexOf(':') + 1).split(' '),
tArr = {}, item; while(item = tValue.shift()) { var title = (receiveCols.length>0 ? 'recv' : 'trans') + '_' + (receiveCols.shift() || transmitCols.shift())
tArr[title] = item;
}
result[tKey] = tArr;
} return result;
}
2 回答
烙印99
TA贡献1829条经验 获得超13个赞
如果就问题而论(匹配lo:就可以了),那本人觉得就对此这个问题没必要使用正则。
下面给我本人的js代码
<script>var str='bytes packets errs drop fifo colls carrier compressed lo: 20114 136 0 0 0 0 0 0 20114 136 0 0 0 0 0 0';var num;var lo;num=str.indexOf('lo:');lo=str.substring(num+3,str.length);alert(lo);</script>
- 2 回答
- 0 关注
- 215 浏览
添加回答
举报
0/150
提交
取消