Hello guys! Today we will share a tricky topic when working with Strings: How to check if String contains only Letters in JavaScript? Let’s learn with us now.
Check if String contains only Letters in JavaScript
Here are three ways we want to introduce to you. Don’t just read. Follow the examples below.
Using regex.test()
function.
We have already introduced the syntax of the regex.test()
function here. You can read more if you want.
First, we’ll use a regular expression: /^[a-zA-Z]+$/
In there:
^
: Beginning of the String.[ ]:
A set of characters inside it- +: Appears one or more times.
a-z:
Matches one of the letters a to z.A-Z:
Matches one of the letters A to Z.$
: End of String.
Then we will call the test()
function and pass the String to be checked into it. It will search for the String corresponding to a Regular Expression inside the String we have just passed. If found, it returns true; otherwise, returns false. Like this:
const username = '[email protected]';
let password = 'learnshareit';
function checkLetters(str) {
// Use test() to check if str contains only letters
let isOnlyLetters = /^[a-zA-Z]+$/.test(str);
if (isOnlyLetters) {
console.log(`${str} contains only Letters!`);
return true;
} else {
console.log(`${str} does not contain only Letters!`);
return false;
}
}
checkLetters(username); // false
checkLetters(password); // true
Output:
[email protected] does not contain only Letters!
learnshareit contains only Letters!
Using string.search()
function.
The search()
function will search for the substring corresponding to a regular expression inside a String. If found, it returns the position of the first character of the substring. Otherwise, it will return -1.
Syntax:
string.search(searchValue);
Parameters:
- string: a given String.
- searchValue: the search value, which can be a string or a regular expression.
We pass the regex used in the first way above into the search()
function to start the search. If it returns the first position of the match, the String we need to check contains only letters. And if it returns – 1, then the String we need to check contains non-alphabetic characters. Observe the example below to understand:
const username = '[email protected]';
let password = 'learnshareit';
function checkLetters(str) {
// Use search() to check if str contains only letters
let position = str.search(/^[a-zA-Z]+$/);
if (position === -1) {
// Position = -1 means a non-alphabetic character exists
console.log(position); // -1
console.log(`${str} does not contain only Letters!`);
return false;
} else {
// Position != -1 means contains only letters
console.log(position); // 0
console.log(`${str} contains only Letters!`);
return true;
}
}
checkLetters(username); // false
checkLetters(password); // true
Output:
-1
[email protected] does not contain only Letters!
0
learnshareit contains only Letters!
Using string.match()
function
We have already introduced the match()
function. If you want to read the syntax of the match()
function, you can read it here.
In this case, we use the regex /^[a-zA-Z]+$/
to pass to the match()
function. It then searches for the substring corresponding to the regex inside the String. If found, it returns an array of matches; otherwise, it will return null.
const username = '[email protected]';
let password = 'learnshareit';
function checkLetters(str) {
// Use search() to check if str contains only letters
let matches = str.match(/^[a-zA-Z]+$/);
if (matches === null) {
// matches = null means a non-alphabetic character exists
console.log(matches); // null
console.log(`${str} does not contain only Letters!`);
return false;
} else {
// matches != null means contains only letters
console.log(matches); // Array
console.log(`${str} contains only Letters!`);
return true;
}
}
checkLetters(username); // false
checkLetters(password); // true
Output:
null
[email protected] does not contain only Letters!
["learnshareit"]
learnshareit contains only Letters!
Summary
So we have introduced some functions to check if String contains only Letters in JavaScript. These functions are widespread when working with Strings. So if you can, you should practice a lot with them. We hope you enjoyed this article.
Have a beautiful day!
Hi, I’m Cora Lopez. I have a passion for teaching programming languages such as Python, Java, Php, Javascript … I’m creating the free python course online. I hope this helps you in your learning journey.
Name of the university: HCMUE
Major: IT
Programming Languages: HTML/CSS/Javascript, PHP/sql/laravel, Python, Java