String Is Alpha

JavaScriptJavaScriptBeginner
Practice Now

This tutorial is from open-source community. Access the source code

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.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL javascript(("`JavaScript`")) -.-> javascript/BasicConceptsGroup(["`Basic Concepts`"]) javascript/BasicConceptsGroup -.-> javascript/variables("`Variables`") javascript/BasicConceptsGroup -.-> javascript/data_types("`Data Types`") javascript/BasicConceptsGroup -.-> javascript/arith_ops("`Arithmetic Operators`") javascript/BasicConceptsGroup -.-> javascript/comp_ops("`Comparison Operators`") subgraph Lab Skills javascript/variables -.-> lab-28408{{"`String Is Alpha`"}} javascript/data_types -.-> lab-28408{{"`String Is Alpha`"}} javascript/arith_ops -.-> lab-28408{{"`String Is Alpha`"}} javascript/comp_ops -.-> lab-28408{{"`String Is Alpha`"}} end

Function to Check if a String is Alpha

To check if a string contains only alphabetic characters:

  • Open the Terminal/SSH and type node to start practicing coding.
  • Use RegExp.prototype.test() to check if the given string matches against the alphabetic regexp pattern.
  • The function isAlpha takes a string as an argument and returns true if the string contains only alphabetic characters, and false otherwise.

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.

Other JavaScript Tutorials you may like