• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / JavaScript / Using JavaScript to Check If String Contains Only Certain Characters

Using JavaScript to Check If String Contains Only Certain Characters

September 26, 2022 Leave a Comment

We can use JavaScript to check if a string contains only certain characters by using nested for loops to check individually if each character is one of those characters or not.

function containsCertainCharacters(theString, chars){
  for( var i=0; i<theString.length; i++ ){
    for( var k=0; k<chars.length; k++ ){
      if(theString.charAt(i) === chars.charAt(k)){
        return true;
      }
    }
  }
  return false;
};

console.log(containsCertainCharacters("Hello World!", "H"));
console.log(containsCertainCharacters("Hello World!", "olz"));
console.log(containsCertainCharacters("Hello World!", "z"));

#Output:
true
true
false

When working with strings, it can be useful to know if there are certain characters contained in a string variable.

In JavaScript, we can easily get if a string contains certain characters in a string by looping over each character of the string and seeing if it is one of the given characters or not.

Below is our function again which will check if a string has certain characters or not for you in a string using JavaScript.

function containsCertainCharacters(theString, chars){
  for( var i=0; i<theString.length; i++ ){
    for( var k=0; k<chars.length; k++ ){
      if(theString.charAt(i) === chars.charAt(k)){
        return true;
      }
    }
  }
  return false;
};

console.log(containsCertainCharacters("Hello World!", "h"));
console.log(containsCertainCharacters("Hello World!", "Wor"));
console.log(containsCertainCharacters("Hello World!", "y"));

#Output:
false
true
false

Checking if Certain Character Appears in a String Using JavaScript

The example above is useful if you want to check if ANY character in your string is in another string. We can also use JavaScript to check if EACH of the characters appear in a string.

To do this, we will loop over the character and if we find a character that is not in the string, we will return false. Otherwise, once we have checked all characters, we will return true.

Below is a JavaScript function that will check if a string has all particular characters.

function containsAllCharacters(theString, chars){
  for( var i=0; i<chars.length; i++ ){
    if((theString.includes(chars[i]))  == false){
      return false;
    }
  }
  return true;
};

console.log(containsAllCharacters("Hello World!", "Hl"));
console.log(containsAllCharacters("This is a string with some words.", "SwDds"));
console.log(containsAllCharacters("What's up?", "Uu"));
console.log(containsAllCharacters("Brrrr", "Pr"));

#Output:
true
false
false
false

Checking if a Vowel Appears in a String Using JavaScript

When working with strings, it can be useful to know if there are any vowels contained in a string variable.

In JavaScript, we can easily check if a string contains vowels by looping over each character of the string and seeing if it is a vowel or not.

We can take the functions from above and modify them slightly to see if there are any vowels in a string.

Below is a JavaScript function that will check if a string has a vowel and return true if it does, and false if it doesn’t. We will use the indexOf() method to help with this.

function containsVowel(theString){
  for( var i=0; i<theString.length; i++ ){
    if("aeiou".indexOf(theString[i]) != -1) {
      return true;
    }
  }
  return false;
};

console.log(containsVowel("Hello World!"));
console.log(containsVowel("This is a string with some words."));
console.log(containsVowel("What's up?"));
console.log(containsVowel("Brrrr"));

#Output:
true
true
true
false

Hopefully this article has been useful for you to use JavaScript to check if a string contains only certain characters.

Other Articles You'll Also Like:

  • 1.  Using JavaScript to Show and Hide a Div
  • 2.  Creating a JavaScript Function to Add Two Numbers
  • 3.  Using JavaScript to Remove Quotes From a String
  • 4.  How to Convert a String to Lowercase In JavaScript
  • 5.  JavaScript Change Text Color of a Paragraph
  • 6.  Using JavaScript to Round to 1 Decimal
  • 7.  How to Convert Degrees to Radians Using JavaScript
  • 8.  Creating a JavaScript Function to Multiply Two Numbers
  • 9.  parseInt() JavaScript – Convert String to Integer with parseInt() method
  • 10.  Using JavaScript to get the Last Element in Array

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy