• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Questions of Javascript Regular Expression

 
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have one problem about JavaScript regular expression which does not work as what I want.
User has reqirement as following:
W = Alpha/Numeric (No Blank)
Y = Alpha/Blank
Z = Numeric/Blank

Now user required the field must match mask:WWWWWYZ, which means
The match value for this should be "12345","12345A", or "12345A9"
or "12345 (2 spaces)","12345A (1 space)", or "12345A9"

I translated the mask to the following javaScript regular expression:
"[a-z0-9][a-z0-9][a-z0-9][a-z0-9][a-z0-9]([a-z]|(\s*))([0-9]|(\s*))"
However, this regular expression match value for "1234567", this should not happen.

If you change regular expression to ]"[a-z0-9][a-z0-9][a-z0-9][a-z0-9][a-z0-9]([a-z]|(\s+))([0-9]|(\s+))", then "12345" could not match.

Anyone in this forum can help me with this? This is really urgent for our project, which is cutting off soon.

Thanks in advance.

Sam
 
author
Posts: 15385
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are going to need a ? for the second half to say this can appear one time

...[a-z0-9](([a-z]|(\s+))([0-9]|(\s+)))?

Eric
 
Sam Sunamin
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Eric,

Thank you very much for your quick reply.

We have the other several other Charaters to stand for other situation:
A -Alpha no blank
X- A-Z,0-9 and variable blanks,
B- blank,
9- 0-9 numeric only
and
W = Alpha/Numeric (No Blank)
Y = Alpha/Blank
Z = Numeric/Blank


I didn't make myself very clear here.

"WWWWWYZ" is not the user's possible mask.

They may have mask whatever they what.

They may have "YZWWZYW" or "AAAXXBBWYZ".

So I have to match character by character.

Can your solution satisfy all different kinds of combination?

I will try you solution like:
...[a-z0-9]([a-z]|(\s+))?([0-9]|(\s+))?


Thank in advance.

Sam
 
Sam Sunamin
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Eric or any expert from this forum,

Can someone help me to solve this problem?

Thanks and regards,

Sam
 
Sam Sunamin
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Eric,

I already tried
...[a-z0-9]([a-z]|(\s+))?([0-9]|(\s+))?

This reg expression will match
'123456', '1234567' for mask 'WWWWWYZ' that is not what I want.

Any idea why? Or what I should do?

Thanks and regards,

Sam
 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sam,
Why do you have to use a regular expression? It seems easier to get the character at each position in the string and compare to the rules for that character.
 
Sam Sunamin
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank Eric and Jeanne for your reply.

The problem is solved. User has special requirement when "X","Y" and "Z" is at the beginning and at the end.

Regards,
Sam
 
reply
    Bookmark Topic Watch Topic
  • New Topic