Grep ping输出仅显示使用javascript的延迟

时间:2016-08-13 05:02:47

标签: javascript regex grep

由此:

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位以上时,它似乎无法正常工作。

1 个答案:

答案 0 :(得分:1)

您可以使用:

/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);