Check if String contains only Letters and Spaces #
To check if a string contains only letters and spaces, call the test[]
method on a regular expression that matches only letters and spaces. The test
method will return true
if the regular
expression is matched in the string and false
otherwise.
Copied!
const str1 = 'only letters and spaces'; const str2 = 'one-two-three'; function onlyLettersAndSpaces[str] { return /^[A-Za-z\s]*$/.test[str]; } console.log[onlyLettersAndSpaces[str1]]; // 👉️ true console.log[onlyLettersAndSpaces[str2]]; // 👉️ false
We called the RegExp.test method to check if the string contains only letters and spaces.
The forward slashes / /
mark the beginning and end of the regular expression.
The caret ^
symbol matches the
beginning of the input and the dollar sign $
matches the end of the input.
The part between the square brackets []
is called a character class and matches all uppercase and lowercase letters and spaces.
The A-Z
and a-z
characters match a range of uppercase and lowercase letters.
The \s
character matches a single whitespace character.
The asterisk *
matches the preceding item [our character class] zero or more times.
If you need help reading a regular expression, check out this regex cheatsheet by the MDN docs.
In its entirety, the regular expression checks if the string contains only uppercase and lowercase letters or spaces from start to finish.
If the condition is met, the test
method returns true
, otherwise it returns
false
.
Here are some more examples:
Copied!
const str1 = 'hello 42'; const str2 = 'one more time'; const str3 = 'Try this.'; function onlyLettersAndSpaces[str] { return /^[A-Za-z\s]*$/.test[str]; } console.log[onlyLettersAndSpaces[str1]]; // 👉️ false console.log[onlyLettersAndSpaces[str2]]; // 👉️ true console.log[onlyLettersAndSpaces[str3]]; // 👉️ false
Further Reading #
- Remove Special Characters from a String in JavaScript
- Check if String contains Special Characters in JavaScript
- Check if String contains only Letters and Numbers in JS
- Check if String contains at least one Number in JavaScript
- Check if String contains at least one Number in JavaScript
- Check if String contains only Digits in JavaScript
- Check if String contains only Latin 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?
Felix Kling
769k171 gold badges1068 silver badges1114 bronze badges
asked May 5, 2014 at 15:43
2
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];
mohagali
1932 silver badges9 bronze badges
answered May 5, 2014 at 15:45
OriolOriol
257k57 gold badges409 silver badges493 bronze badges
0
The fastest way is to check if there is a non letter:
if [!/[^a-zA-Z]/.test[word]]
answered May 5, 2014 at 15:50
2
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 $
]
answered May 5, 2014 at 15:46
JDBJDB
23.9k5 gold badges71 silver badges117 bronze badges
Try this
var Regex='/^[^a-zA-Z]*$/';
if[Regex.test[word]]
{
//...
}
I think it will be working for you.
answered Sep 22, 2017 at 6:55
1
try to add \S at your pattern
^[A-Za-z]\S*$
answered May 5, 2014 at 15:46
ToninoTonino
1,01010 silver badges22 bronze badges