aaarghHHH!!! regexp

M

Marco Snoek

i just have to accept I can't handle regexp.. =[

Have to check a licence plate in a form, always consisting out of:
three times (2 numbers or 2 charakters), seperated by a -

so: jb-42-zd
or: 93-ad-87

etc...

Any help???
 
M

Martin Honnen

Marco said:
i just have to accept I can't handle regexp.. =[

Have to check a licence plate in a form, always consisting out of:
three times (2 numbers or 2 charakters), seperated by a -

so: jb-42-zd
or: 93-ad-87

var plates = [
'jb-42-zd',
'93-ad-87'
];
var platePattern = /^[a-z0-9]{2}-[a-z0-9]{2}-[a-z0-9]{2}$/i;
for (var i = 0; i < plates.length; i++) {
alert('plate: ' + plates + ' matches pattern: ' + platePattern +
': ' + platePattern.test(plates))
}

The pattern allows for both upper case and lower case letters, remove
the /i if you don't want that.
 
F

Fred Serry

Martin Honnen said:
Marco said:
i just have to accept I can't handle regexp.. =[

Have to check a licence plate in a form, always consisting out of:
three times (2 numbers or 2 charakters), seperated by a -

so: jb-42-zd
or: 93-ad-87

var plates = [
'jb-42-zd',
'93-ad-87'
];
var platePattern = /^[a-z0-9]{2}-[a-z0-9]{2}-[a-z0-9]{2}$/i;
for (var i = 0; i < plates.length; i++) {
alert('plate: ' + plates + ' matches pattern: ' + platePattern +
': ' + platePattern.test(plates))
}

The pattern allows for both upper case and lower case letters, remove
the /i if you don't want that.


Hi,

Assuming that Dutch plates are in question, OP forgat to mention that
the groups of two chars may never consist of a combination of letters
and numbers.

Martin's regexp adjusted for that fact:

var plates = [
'jb-42-zd',
'93-ad-87'
];
var platePattern =
/^([a-z]{2}|[0-9]{2})-([a-z]{2}|[0-9]{2})-([a-z]{2}|[0-9]{2})$/i;
for (var i = 0; i < plates.length; i++) {
alert('plate: ' + plates + ' matches pattern: ' + platePattern +
': ' + platePattern.test(plates))
}

Fred
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,099
Messages
2,570,626
Members
47,237
Latest member
David123

Latest Threads

Top