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 關注
- 274 瀏覽
添加回答
舉報
0/150
提交
取消