MAC Address Formatting

jevans
Kilo Contributor

I've seen some inquiries and information related to MAC address transformation/normalization, but I'm not exactly the best at regular expressions so I'm having trouble getting this to work.

My instance contains several forms (including the Incident form) which include fields for a MAC address. While most people separate pairs of hex characters with colons, I've seen other formatting (dashes, periods, etc), and generally we like to remove all special characters and display only 12 hexadecimal characters (we have several other systems which this works universally for).

I configured a transformation which includes 5 transforms to remove up to 5 colons. Not only is this tedious, but I'd have to set up transforms for other special characters that may be used, and repeat the process for the multiple forms we include MAC addresses on.

Is there a simpler way to have the system strip out all special characters, and confirm the remaining characters are all hexadecimal?

I believe the regular expression I'm looking for is the following, but I'm not sure how to best apply it:
/([0-9a-fA-F]{12})/i

1 ACCEPTED SOLUTION

Here is a simple client script friendly replace function that you could use instead:



var macString = "01 23:45F7.89-aB";
macString = macString.replace(/\W/gi,"");


This code example will remove any non-alphanumeric character from the string and return a variable with just the characters that matter for the MAC Address.


View solution in original post

5 REPLIES 5

john_andersen
Tera Guru

The following script worked for me...I was in a hurry, so I didn't streamline it, but wanted to give you an idea:



//Using a mac Address string that uses Space, Colon, Period, Hyphen, and nothing between each segment
//of the address to test the various possible delimiter tokens.
var macString = "01 23:45F7.89-aB";

var tokens = /([0-9a-fA-F]{2})\W{0,1}([0-9a-fA-F]{2})\W{0,1}([0-9a-fA-F]{2})\W{0,1}([0-9a-fA-F]{2})\W{0,1}([0-9a-fA-F]{2})\W{0,1}([0-9a-fA-F]{2})/(macString);

if(tokens && tokens.length == 7){
gs.log("MAC Address: " + tokens[1]+tokens[2]+tokens[3]+tokens[4]+tokens[5]+tokens[6]);
}


That code, when executed, gives me:



*** Script: MAC Address: 012345F789aB


This works perfectly as a background script, but the line:
var tokens = /([0-9a-fA-F]{2})\W{0,1}([0-9a-fA-F]{2})\W{0,1}([0-9a-fA-F]{2})\W{0,1}([0-9a-fA-F]{2})\W{0,1}([0-9a-fA-F]{2})\W{0,1}([0-9a-fA-F]{2})/(macString);

seems to give me the following error:

Error:

Problem at line 1 character 1: Bad invocation.

var u_incidentMAC = Class.create();


When I remove that line the error goes away. I've tried this using client scripts and using client callable script includes. I get the same results with both.

Any suggestions?


Here is a simple client script friendly replace function that you could use instead:



var macString = "01 23:45F7.89-aB";
macString = macString.replace(/\W/gi,"");


This code example will remove any non-alphanumeric character from the string and return a variable with just the characters that matter for the MAC Address.


Brilliant!

This works quite nicely.

Thank you very much!