Javascript:使用Regex从自由格式文本中提取MAC代码 [英] Javascript: Extract MAC codes from freeform text using Regex

查看:81
本文介绍了Javascript:使用Regex从自由格式文本中提取MAC代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个Chrome扩展程序,它会扫描网页上的有效MAC代码并将每个扩展到一个数组。我有一个验证MAC的正则表达式,但我无法匹配多个代码。

I'm attempting to write a Chrome Extension that will scan a web page for valid MAC codes and push each in to an array. I have a regex that validates the MAC but I can't get it to match multiple codes.

以下是我的代码的快速摘录:

Here is a quick snippet of my code :

var regex = /^([0-9A-F]{2}[:-]){5}([0-9A-F]{2})$/;
var body = document.body.innerText.replace(/\n/g,' ');

console.log(body.match(regex));

var occur = regex.exec(body);

console.log(occur);

非常感谢。

推荐答案


MAC地址通常是6组,每组两个十六进制数字(0-9,A,B,C,D,E,F),用冒号(:)或连字符( - )。

MAC addresses are typically 6 groups of two hexadecimal digits (0-9,A,B,C,D,E,F), separated either by colons (:) or hyphens (-).

您在寻找这个吗?

/([0-9A-F]{2}(?:[:-][0-9A-F]{2}){5})/g

在线演示

从索引1获取匹配的组。

Get the matched group from index 1.

这篇关于Javascript:使用Regex从自由格式文本中提取MAC代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆