reg exp

G

Gaijinco

I'm trying to have a regexp to validate string that contain letters
numbers and the character ' ' but that begins with a letter

I'm using:

[a-z,A-Z][a-z,A-Z,0-9, ]*

But it does is validated: aaa !!! aaa

What I'm doing wrong?

Thanks
 
T

Thomas 'PointedEars' Lahn

Gaijinco said:
I'm trying to have a regexp to validate string that contain letters
numbers and the character ' ' but that begins with a letter

I'm using:

[a-z,A-Z][a-z,A-Z,0-9, ]*

But it does is validated: aaa !!! aaa

What I'm doing wrong?

In "aaa !!! aaa", /[a-z,A-Z][a-z,A-Z,0-9, ]*/ matches "aaa ", so the
expression does match the input, indeed: /[a-z,A-Z]/ matches the first `a';
/[a-z,A-Z,0-9, ]*/ matches the other two `a' and the following space character.

You need to anchor your expression so that the whole input must conform to
the format. And using the comma in there matches the comma; you should
remove it.

/^[a-z][\w ]*$/i

might be what you are looking for.


PointedEars
 
D

Dr J R Stockton

In comp.lang.javascript message <[email protected]>, Wed,
2 Jul 2008 18:05:46 said:
/^[a-z][\w ]*$/i

might be what you are looking for.

That will accept underlines. IIRC, I have told you about that
previously.

It's a good idea to read the newsgroup c.l.j and its FAQ. See below.
 
G

Gregor Kofler

Gaijinco meinte:
I'm trying to have a regexp to validate string that contain letters
numbers and the character ' ' but that begins with a letter

I'm using:

[a-z,A-Z][a-z,A-Z,0-9, ]*

Why the colons? You don't want to match ",", do you?

/^[a-z][a-z0-9 ]*$/i will do the job.

But it does is validated: aaa !!! aaa

Your expression matches the first group of a's. That's sufficient for a
match.

Gregor
 
T

Thomas 'PointedEars' Lahn

Gregor said:
Gaijinco meinte:
I'm trying to have a regexp to validate string that contain letters
numbers and the character ' ' but that begins with a letter

I'm using:

[a-z,A-Z][a-z,A-Z,0-9, ]*

Why the colons? You don't want to match ",", do you?
^^^^^^
colon: `:'
comma: `,'

(SCNR)
/^[a-z][a-z0-9 ]*$/i will do the job.

Right. \w, as I had suggested, includes the underscore ("_").


PointedEars
 
T

Tim Slattery

Gaijinco said:
I'm trying to have a regexp to validate string that contain letters
numbers and the character ' ' but that begins with a letter

I'm using:

[a-z,A-Z][a-z,A-Z,0-9, ]*

But it does is validated: aaa !!! aaa

That RE is looking for *any* part of your string to match. In order to
force it to match the *entire* string, surround it with beginning of
line/end of line match characters. And drop the commas if you don't
want the RE to match them.

^[a-zA-Z][a-zA-Z0-9 ]*$
 
G

Gregor Kofler

Thomas 'PointedEars' Lahn meinte:
Gregor said:
Gaijinco meinte:
I'm trying to have a regexp to validate string that contain letters
numbers and the character ' ' but that begins with a letter

I'm using:

[a-z,A-Z][a-z,A-Z,0-9, ]*
Why the colons? You don't want to match ",", do you?
^^^^^^
colon: `:'
comma: `,'

(SCNR)

Damn! Ah well, I was wondering what the "semi" in "semicolon" was
referring to...

Gregor
 
T

Thomas 'PointedEars' Lahn

Gregor said:
Thomas 'PointedEars' Lahn meinte:
Gregor said:
Gaijinco meinte:
[a-z,A-Z][a-z,A-Z,0-9, ]*
Why the colons? You don't want to match ",", do you?
^^^^^^
colon: `:'
comma: `,'

(SCNR)

Damn! Ah well, I was wondering what the "semi" in "semicolon" was
referring to...

And it becomes even more interesting when you consider where "colon"
comes from. (I have always found etymology to be a quite fascinating subject.)

<http://en.wikipedia.org/wiki/Colon_(punctuation)#History>


Regards,

PointedEars, F'up2p
 

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

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top