tags:

views:

484

answers:

5

Hello I was writing a Regular Expression (first time in my life I might add) but I just can't figure out how to do what I want. So far, so good since I already allow only Letters and spaces (as long as it's not the first character) now what I'm missing is that I don't want to allow any numbers in between the characters...could anybody help me please?

/^[^\s][\sa-zA-Z]+[^\d\W]/
A: 

regexlib.com is your friend.

Kon
+1  A: 

If you only want to allow letters and spaces, then what you have is almost correct:

/^[a-zA-Z][\sa-zA-Z]*$/

The $ at the end signifies the end of the string.

Edited to correct answer, thanks to @Alnitak

Graeme Perrow
+4  A: 

OK, what you need is:

/^[a-zA-Z][\sa-zA-Z]*$/

This matches:

^           - start of line
[a-zA-Z]    - any letter
[\sa-zA-Z]* - zero or more letters or spaces
$           - the end of the line

If you want to ensure that it also ends in a letter then put another

[a-zA-Z]

before the $. Note however that the string will then have to contain at least two letters (one at each end) to match.

Alnitak
A: 

Thank you =D I also made a note of the webpage. Thanks =)

Luis Armando
This should be a comment or an edit to your question, not an answer.
Geoffrey Chetwood
A: 

If you want to ensure that spaces occur only between words, use this:

/^[A-Za-z]+(?:\s+[A-Za-z]+)*$/
Alan Moore