JavaScript解析字符串并提取数字
我想从以下字符串中获取发送总数、成功总数和错误总数JavaScript解析字符串并提取数字,javascript,Javascript,我想从以下字符串中获取发送总数、成功总数和错误总数 03/07/2017 17:11:28.5323至www.google.com:65.199.32.57 65.199.32.20 65.246.5.55 65.199.32.58总发送:107总成功:107总错误:0:11:93:15.32/ 到目前为止,我已经尝试了以下方法,但还没有成功。 变量res保存在字符串上方 下面的结果是错误的 “TypeError:无法读取null的属性“1” 这将导致字符串中包含所有数字的串联数组,这不是我想要
03/07/2017 17:11:28.5323至www.google.com:65.199.32.57 65.199.32.20 65.246.5.55 65.199.32.58总发送:107总成功:107总错误:0:11:93:15.32/
到目前为止,我已经尝试了以下方法,但还没有成功。
变量res保存在字符串上方
下面的结果是错误的
“TypeError:无法读取null的属性“1”
这将导致字符串中包含所有数字的串联数组,这不是我想要的
var numberPattern = /\d+/g;
var numArr = res.match(numberPattern);
console.log(numArr);
我想要
Total Sent = 107
Total Success = 107
Total Error = 0.
你可以试试这个功能
例如:
var String=str.substring(str.lastIndexOf(“发送总数”)+1,str.lastIndexOf(“”)//要获取总发送值,
/(\d+)总发送值(\d+)/
不匹配,因为总发送值
前面或后面没有数字。您希望发送/Total:(\d+/
@FelixKling)谢谢您的帮助。如果将答案格式化为代码,并解释您使用的方法,则此答案会更好。
var input = '03/07/2017 17:11:28.5323 to www.google.com : 65.199.32.57 65.199.32.20 65.246.5.55 65.199.32.58 Total Sent: 107 Total Success: 107 Total Errors: 0 0 : 11 : 93 : 15.32/';
var match = input.match(/Total (\w+): (\d+)/g);
console.log(match);
// ["Total Sent: 107", "Total Success: 107", "Total Errors: 0"]
var output = match.join('\n').replace(/\:/g, ' =');
console.log(output);
/*
Total Sent = 107
Total Success = 107
Total Errors = 0
*/
var input = '03/07/2017 17:11:28.5323 to www.google.com : 65.199.32.57 65.199.32.20 65.246.5.55 65.199.32.58 Total Sent: 107 Total Success: 107 Total Errors: 0 0 : 11 : 93 : 15.32/';
var match = input.match(/Total (\w+): (\d+)/g);
console.log(match);
// ["Total Sent: 107", "Total Success: 107", "Total Errors: 0"]
var output = match.join('\n').replace(/\:/g, ' =');
console.log(output);
/*
Total Sent = 107
Total Success = 107
Total Errors = 0
*/