/    Sign up×
Community /Pin to ProfileBookmark

regular expressions challenge!

I’m trying to build a regular expression for Canadian postal codes for javascript but I’m not really up to the task. Can anyone here do it?

It needs to be:
letter (upper case only)
number
letter (upper case only)
no space OR space OR dash
number
letter (upper case only)
number
And then no more.

What would this regular expression look like?

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@steelersfan88Jul 06.2004 — Maybe somehting like:<script type="text/javascript">

/* Requirements:
> letter (upper case only)
> number
> letter (upper case only)
> no space OR space OR dash
> number
> letter (upper case only)
> number
> And then no more.
*/

var test_str = "A2A-2A2"
var reg_exp = /[A-Z]d[A-Z](s?|-)d[A-Z]d$/

alert(reg_exp.test(test_str))

/* online examples:
1 >> /^([a-z]d){3}$/i
2 >> /^[a-cehj-npr-tvxy]d[a-z](s)?d[a-z]d$/i
*/

</script>
Dr. Script
Copy linkTweet thisAlerts:
@tachyonauthorJul 06.2004 — Cool! Thanks!
Copy linkTweet thisAlerts:
@steelersfan88Jul 06.2004 — Sure thing ?
×

Success!

Help @tachyon spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...