由此:
PING google.com (74.125.68.138) 56(84) bytes of data.
64 bytes from sc-in-f138.1e100.net (74.125.68.138): icmp_seq=1 ttl=48 time=76.8 ms
64 bytes from sc-in-f138.1e100.net (74.125.68.138): icmp_seq=2 ttl=48 time=86.8 ms
我需要使用javascript分隔此部分:
76.8
86.8
到目前为止,我想到了这个:
'([0-9][0-9][0-9].[0-9] ms|[0-9][0-9].[0-9] ms|[0-9].[0-9] ms)'
但是当延迟超过2位数时,它似乎无法正常工作。
最佳答案
您可以使用:
/time=(\d+(\.\d+)?) ms$/ // 1: one digit or more
\_/\______/ // 2: optionally followed by a dot and one digit or more
1 2 // $: end of input
var ping =
"64 bytes from sc-in-f138.1e100.net (74.125.68.138): icmp_seq=1 ttl=48 time=76.8 ms";
var time = Number(ping.match(/time=(\d+(\.\d+)?) ms$/)[1]);
console.log(time);
关于javascript - Grep ping输出仅使用javascript显示延迟,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/38929392/