【问题标题】:How would I strip something from being encased between colons and retrieve the last one on it我如何从冒号之间剥离一些东西并检索它的最后一个
【发布时间】:2020-11-26 18:55:05
【问题描述】:

我一直在不知疲倦地研究这个 pcap 分析脚本,终于得到了“某个地方”,但现在我遇到了从用冒号括起来的行中剥离协议的问题:

IE:

eth:ethertype:arp

eth:ethertype:ip:tcp:ssh

我正在尝试从 ssharp 等每个对象中获取最后一个值,这些对象的大小会发生变化(使用 tshark pcap - JSON 文件)

【问题讨论】:

  • 字符串在: char 处拆分,然后是array[array.length-1]

标签: node.js json pcap tshark


【解决方案1】:

有多种方法可以做到这一点。您可以使用.split(":"),然后从结果数组中取出最后一项。

let str = "eth:ethertype:ip:tcp:ssh";

let splits = str.split(":");
console.log(splits[splits.length - 1]);

您可以使用如下正则表达式:

let str = "eth:ethertype:ip:tcp:ssh";
let regex = /:([^:]+)$/;
let matches = str.match(regex);
if (matches) {
    console.log(matches[1]);
} else {
    console.log("no matches");
}

【讨论】:

  • 大声笑,我正要问你什么时候第一次回答.split(":") 的意思,但是当我点击评论时,我更新了你的答案。在现场开发中对其进行了测试,并像魅力一样工作,感谢您的提示;必须记住这一点以备将来参考。 @jfriend00
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-06
  • 2018-11-13
  • 1970-01-01
  • 1970-01-01
  • 2022-09-23
  • 1970-01-01
相关资源
最近更新 更多