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

标签 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)'

但是当延迟达到两位数以上时,它似乎无法正常工作。

最佳答案

您可以使用:

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

相关文章:

BASH:在文件中搜索几个关键字并突出显示它们

javascript - 如何发布我在 Rails 中看到的一些问题的 jsfiddle 示例?

javascript - 如何为内容可编辑元素上的特定单词着色?

javascript - JSON 数据上的 Angularjs

python - 解析 Python 的输出

bash - grep 查找单词,如果匹配则获取括号之间的内容

javascript - 为什么我的游戏在 Firefox 中那么慢?

regex - 在终端中使用 RegEx 从字符串中提取字符串

php - 如何实现像 www.example.com/username 这样的 url 以重定向到 www.example.com/user/profile.php?uid=10?

linux - 如何使用 awk 按字母或结束字符分隔字段