[javascript] Check if string contains only letters in javascript

So I tried this:

if (/^[a-zA-Z]/.test(word)) {
   // code
}

It doesn't accept this : " "

But it does accept this: "word word", which does contain a space :/

Is there a good way to do this?

This question is related to javascript regex

The answer is


try to add \S at your pattern

^[A-Za-z]\S*$


You need

/^[a-zA-Z]+$/

Currently, you are matching a single character at the start of the input. If your goal is to match letter characters (one or more) from start to finish, then you need to repeat the a-z character match (using +) and specify that you want to match all the way to the end (via $)


The fastest way is to check if there is a non letter:

if (!/[^a-zA-Z]/.test(word))

Try this

var Regex='/^[^a-zA-Z]*$/';

 if(Regex.test(word))
 {
 //...
 }

I think it will be working for you.