Javascript Replace with RegEx...?

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
s0h0
Forum Newbie
Posts: 21
Joined: Fri Feb 27, 2009 4:09 am

Javascript Replace with RegEx...?

Post 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!
mintedjo
Forum Contributor
Posts: 153
Joined: Wed Nov 19, 2008 6:23 am

Re: Javascript Replace with RegEx...?

Post by mintedjo »

Can you give some examples of expected input that needs correcting and the desired outcome?
s0h0
Forum Newbie
Posts: 21
Joined: Fri Feb 27, 2009 4:09 am

Re: Javascript Replace with RegEx...?

Post 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;
           }
        }
User avatar
prometheuzz
Forum Regular
Posts: 779
Joined: Fri Apr 04, 2008 5:51 am

Re: Javascript Replace with RegEx...?

Post 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-");
s0h0
Forum Newbie
Posts: 21
Joined: Fri Feb 27, 2009 4:09 am

Re: Javascript Replace with RegEx...?

Post 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 :))
User avatar
prometheuzz
Forum Regular
Posts: 779
Joined: Fri Apr 04, 2008 5:51 am

Re: Javascript Replace with RegEx...?

Post 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
s0h0
Forum Newbie
Posts: 21
Joined: Fri Feb 27, 2009 4:09 am

Re: Javascript Replace with RegEx...?

Post 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();
Post Reply