aspose file tools*
The moose likes HTML, CSS and JavaScript and the fly likes Questions of Javascript Regular Expression Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "Questions of Javascript Regular Expression" Watch "Questions of Javascript Regular Expression" New topic
Author

Questions of Javascript Regular Expression

Sam Sunamin
Ranch Hand

Joined: Mar 16, 2007
Posts: 113
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


Yours Sam<br />SCJP5.0 97%<br />SCBCD5.0 72%
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
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

Joined: Mar 16, 2007
Posts: 113
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

Joined: Mar 16, 2007
Posts: 113
Hi Eric or any expert from this forum,

Can someone help me to solve this problem?

Thanks and regards,

Sam
Sam Sunamin
Ranch Hand

Joined: Mar 16, 2007
Posts: 113
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
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 30938
    
158

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.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Sam Sunamin
Ranch Hand

Joined: Mar 16, 2007
Posts: 113
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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Questions of Javascript Regular Expression