Regex for letters only
^[a-zA-Z]+$ matches a string of ASCII letters only. For letters in any language use ^\p{L}+$ with the u flag in JavaScript (or [^\W\d_]+ in engines without \p). To allow names with spaces, use ^[a-zA-Z]+(?: [a-zA-Z]+)*$ so the spaces must sit between words.
Syntax
/^[a-zA-Z]+$/ or /^\p{L}+$/uExamples
["Hello", "Hello1", "José"].map(s => /^[a-zA-Z]+$/.test(s))Result: [true, false, false]
Accented letters are outside a to z.
/^\p{L}+$/u.test("José")Result: true
\p{L} is any Unicode letter; needs the u flag.
"Hello, World 42".replace(/[^a-zA-Z]/g, "")Result: "HelloWorld"
["Mary Ann", " Mary", "Mary Ann"].map(s => /^[a-zA-Z]+(?: [a-zA-Z]+)*$/.test(s))Result: [true, false, false]
Single spaces between words only; no leading or double spaces.
Common mistakes
- Using \w, which also matches digits and underscore.
- Writing [a-Z], which is an invalid range because Z comes before a in ASCII; JavaScript throws a SyntaxError.
- Rejecting names like Zoë or Müller with [a-zA-Z]; use \p{L} with the u flag when input is international.
- Adding \s to the class to allow spaces, which also lets newlines and tabs through.
Practice Regex for letters only
These drills check your answer on live data, free, no account needed.
- Ranges: a letter and two digitsThe whole line must match. Match one lowercase letter followed by exactly two digits, like a12.
- Character class: gray or greyThe whole line must match. Match both spellings, gray and grey, and nothing else.
- Unicode letters with the u flagThe whole line must match. Match a single word made only of letters from any language, such as café or Zoë. Digits, spaces and punctuation are not allowed.
Questions
What is the regex for alphabets only with spaces?
^[a-zA-Z ]+$ allows letters and spaces anywhere. ^[a-zA-Z]+(?: [a-zA-Z]+)*$ is stricter: single spaces between words, none at the ends.
How do I match uppercase letters only?
^[A-Z]+$. For Unicode uppercase use ^\p{Lu}+$ with the u flag.
Should I validate a person's name with a letters-only regex?
Usually not. Real names contain apostrophes, hyphens, spaces and letters from many scripts. If you must check, use a loose pattern such as ^[\p{L}' -]+$ with the u flag.
Related
- Regex for alphanumericThe ^[a-zA-Z0-9]+$ pattern, how it differs from \w, how to make a URL slug by replacing everything else, and a Unicode version with \p{L} and \p{N}.
- Regex for numbers onlyThe ^\d+$ pattern for digits-only strings, stripping everything that is not a digit, extracting numbers from text, and patterns for signed and decimal values.
- Unicode regexHow the u flag changes JavaScript regex, matching any letter with \p{L}, a script with \p{Script=...}, emoji as one character, and ASCII limits of \w and \b.
- Ranges in character classesHow character ranges work by code point, how to combine them, the invalid [a-Z] and surprising [A-z], and how to express numeric ranges with alternation.
Previous: Regex for numbers onlyNext: Regex for alphanumeric