Introduction
In this lab, we will be exploring how to determine if a given string contains only alphabetic characters using JavaScript. We will use a regular expression pattern to test the input string and return a boolean value indicating whether the string contains only alphabetic characters or not. This lab aims to help you strengthen your understanding of regular expressions and their usage in JavaScript.
Function to Check if a String is Alpha
To check if a string contains only alphabetic characters:
- Open the Terminal/SSH and type
nodeto start practicing coding. - Use
RegExp.prototype.test()to check if the given string matches against the alphabetic regexp pattern. - The function
isAlphatakes a string as an argument and returnstrueif the string contains only alphabetic characters, andfalseotherwise.
Here's an example:
const isAlpha = (str) => /^[a-zA-Z]*$/.test(str);
isAlpha("sampleInput"); // true
isAlpha("this Will fail"); // false
isAlpha("123"); // false
Summary
Congratulations! You have completed the String Is Alpha lab. You can practice more labs in LabEx to improve your skills.