[英]Regex for Mac address not working properly
我正在使用 javascript 嘗試 grep 不是 mac 地址的所有內容並將其刪除。 這是一個字符串的例子。
var x = "20:ce:c4:00:01:e6 VLAN_ID=0";
我正在嘗試刪除"VLAN_ID=0"
所以所需的 output 將是:
var x ="20:ce:c4:00:01:e6"
我為 mac 地址使用了兩種不同的正則表達式:
const regex = /[0-9a-f]{1,2}([\\.:-])(?:[0-9a-f]{1,2}\1){4}[0-9a-f]+/;
const regex2 = /([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})|([0-9a-fA-F]{4}\\.[0-9a-fA-F]{4}\\.[0-9a-fA-F]{4})+/;
我試過玩 ^ 但沒有運氣使用x = x.replace(regex2, "");
它用 output VLAN_ID=0
替換了給我留下的所有 mac 地址
您可以像這樣訪問第一個匹配項來提取結果:
var x = "20:ce:c4:00:01:e6 VLAN_ID=0"; const regex2 = /([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})|([0-9a-fA-F]{4}\\.[0-9a-fA-F]{4}\\.[0-9a-fA-F]{4})+/; x = x.match(regex2)[0]; console.log(x)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.