javascript - How to extract information out of multi-line string? -
i have string:
1970/11/05 18:40:06 transferred: 484844 bytes (4745455 bytes/s) errors: 46 checks: 5550 transferred: 5450 elapsed time: 8.6s
i want extract information (except data) out of string , save object. problem regex indent of value, because spaces between label , value smaller if value gets bigger. way regex?
i think shouldn't use regex, simple split()
, map()
. parsed data map find nice use stuff that, though use object...
var str = `1970/11/05 18:40:06 transferred: 484844 bytes (4745455 bytes/s) errors: 46 checks: 5550 transferred: 5450 elapsed time: 8.6s`; var splitted = str.split("\n"); splitted.shift(); var data = new map(splitted.map(v => { var f = v.replace(/\s/gm, "").split(":"); return [f[0], f[1]] })); console.log(data.get("errors")); // 46
if prefer solution parse object:
var obj = {}; splitted.foreach(function(v) { var f = v.replace(/\s/gm, "").split(":"); obj[f[0]] = f[1]; }); console.log(obj.errors)
Comments
Post a Comment