Page 1 of 1

Javascript Replace with RegEx...?

Posted: Wed Mar 04, 2009 9:09 am
by s0h0
Hello Egypts,

i have a lil problem. I try to correct the users input for a MAC Address with Javascript. On Textinput, after every second character there should be a "-" inserted. That works not hundred percent, if there where entered less then 12 characters then 5 minus will be inserted after the input.

Now i will remove them with the Javascript Replace() function. Therefore i need a RegEx that will find one or more "-".

But i couldnt build one that works... I dont know why this one wont work.

Code: Select all

/\\-{1,5}$/g
Has anyone an idea?


Thank u very much so far im a real RegEx noob!

Re: Javascript Replace with RegEx...?

Posted: Wed Mar 04, 2009 9:37 am
by mintedjo
Can you give some examples of expected input that needs correcting and the desired outcome?

Re: Javascript Replace with RegEx...?

Posted: Wed Mar 04, 2009 9:42 am
by s0h0
yes, sure.

the user should be able to enter parts of a MAC like:

A5DF6
or
GF:D8:C7:H1

and it should corrected to

A5-DF-6
GF-D8-C7-H1

at the moment it looks lie that:
GF-HG-HG---


thats my whole JS function...

Code: Select all

function checkMAC(e) 
        {
            var macfield = document.getElementById('ctl00_cphMain_tbMAC');
            if (macfield.value != '') {
                mac = macfield.value.replace(/[ |:|.|\\-]/g, '');
                mac = mac.substring(0, 2) + '-' + mac.substring(2, 4) + '-' + mac.substring(4, 6) + '-' + mac.substring(6, 8) + '-' + mac.substring(8, 10) + '-' + mac.substring(10, 12);
 
                mac = mac.replace(/\\-{1,5}$/g, '');   //  <----  This is the Part that dont work...
                mac = mac.toUpperCase();
                macfield.value = mac;
           }
        }

Re: Javascript Replace with RegEx...?

Posted: Wed Mar 04, 2009 9:57 am
by prometheuzz

Code: Select all

var regex = /([a-zA-Z0-9]{2})[^a-zA-Z0-9]?(?!$)/g;
var str = "GF:D8:C7:H1";
str = str.replace(regex, "$1-");

Re: Javascript Replace with RegEx...?

Posted: Wed Mar 04, 2009 10:06 am
by s0h0
thank u for ur reply.

it keeps standing in this way...

HJ-HJ-H---




what should the $1- do? if i replace it with '' everything blows up :))

Re: Javascript Replace with RegEx...?

Posted: Wed Mar 04, 2009 10:19 am
by prometheuzz
s0h0 wrote:thank u for ur reply.

it keeps standing in this way...

HJ-HJ-H---
I tested it with the two examples you gave:

Code: Select all

A5DF6
GF:D8:C7:H1
and they came out the way you wanted:

Code: Select all

A5-DF-6 
GF-D8-C7-H1

s0h0 wrote:what should the $1- do? if i replace it with '' everything blows up :))
The $1 is replaced with what the regex-pattern has grouped into it's first group.
If you don't know what "grouping" means in regex, read this excellent article: http://www.regular-expressions.info/brackets.html

Re: Javascript Replace with RegEx...?

Posted: Thu Mar 05, 2009 3:58 am
by s0h0
ah ok.

now it works for me too... thank u!

mac = mac.replace(/[ :.-]/g, '');
mac = mac.substring(0, 2) + '-' + mac.substring(2, 4) + '-' + mac.substring(4, 6) + '-' + mac.substring(6, 8) + '-' + mac.substring(8, 10) + '-' + mac.substring(10, 12);
mac = mac.replace(/\-{1,5}$/g, '').toUpperCase();