83

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?

2
  • 2
    How do you want empty strings to be handled? Commented May 5, 2014 at 15:48
  • are you wanting to check for letters only or word characters and spaces? Even with the pattern fixed it will return false for "word word" Commented May 5, 2014 at 15:48

6 Answers 6

159

With /^[a-zA-Z]/ you only check the first character:

  • ^: Assert position at the beginning of the string
  • [a-zA-Z]: Match a single character present in the list below:
    • a-z: A character in the range between "a" and "z"
    • A-Z: A character in the range between "A" and "Z"

If you want to check if all characters are letters, use this instead:

/^[a-zA-Z]+$/.test(str);
  • ^: Assert position at the beginning of the string
  • [a-zA-Z]: Match a single character present in the list below:
    • +: Between one and unlimited times, as many as possible, giving back as needed (greedy)
    • a-z: A character in the range between "a" and "z"
    • A-Z: A character in the range between "A" and "Z"
  • $: Assert position at the end of the string (or before the line break at the end of the string, if any)

Or, using the case-insensitive flag i, you could simplify it to

/^[a-z]+$/i.test(str);

Or, since you only want to test, and not match, you could check for the opposite, and negate it:

!/[^a-z]/i.test(str);
Sign up to request clarification or add additional context in comments.

Comments

22

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

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

2 Comments

it should be !/^[a-zA-Z]/
@wrangler: no, if you write that, you will only check if the string doesn't start with a letter.
13

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 $)

Comments

3

Try this

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

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

I think it will be working for you.

1 Comment

Hi, I think you should check again if this solution works. In my opinion there is no point using ^ twice and you should delete second one. You should also change "*" to "+" otherwise function will return true when word will be empty string or some amount of spaces. Correct me if I'm wrong :D.
0

try to add \S at your pattern

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

Comments

0

Here is the code to check the string:

/**
 * Check null, should contain only letters, allowed space, min length is minLength. 
 * @param minLength 
 * @param string 
 * @returns 
 */
export const isStringInValid = (string: string, minLength: number) => {
    return !string || !string?.trim() || !/^[a-zA-Z ]+$/.test(string) || string.length < minLength
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.