How do I turn off numbers in a regular expression?

Hello I am writing a regex (first time in my life I might add) but I just cannot figure out how to do what I want. So far so good that I already only allow letters and spaces (until this is the first character), now I don't understand that I don't want to allow any numbers between characters ... can someone help me please?

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

      

+1


source to share


3 answers


OK, you need:

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

      

It corresponds:

^           - 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 it to end with a letter too, then put another one

[a-zA-Z]

      

before $

. Note, however, that the string must contain at least two letters (one at each end) to match.

+4


source


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

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

      



$

at the end means the end of the line.

Edited to correct the answer, thanks to @Alnitak

+1


source


If you only want spaces between words, use this:

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

      

+1


source







All Articles