Check if String Contains Letters in JavaScript

Check if String Contains Letters in JavaScript | The includes() function, indexOf(), or a regular expression can all be used to determine whether a string of JavaScript contains a specific character or phrase. The most popular and intended way for determining whether a string contains a letter or group of characters is the includes() method.

In any computer language, determining if a string includes a substring is a common task. Let’s take the example of creating an online game. To ensure that every username is appropriate for your game, you might want to check to see if it contains a word that is forbidden.

There are three ways to determine whether a string of JavaScript comprises another character or series of characters:-

  1. includes()
  2. indexOf()
  3. Regular expressions (regex)

These three ways will be covered in this tutorial’s discussion of how to determine whether a JavaScript string includes another string.

Check if String Contains Letters in JavaScript Using includes()

The includes() method in JavaScript, which was added in ES6, check to see if a string contains the characters you supplied it. The method will return “true” if a specific set of characters are present in the string. The contains() will return “false” if the given string does not include the characters you are searching for.

The includes() method’s syntax is as follows:-
string.includes(word);

The term “string” refers to the characters we will search through. The characters we are searching for are referred to as “word.” Here is an illustration of how to use the includes() method:-

let example = "JavaScript String!";
let ourSubstring = "JavaScript";

if (example.includes(ourSubstring)) {
    console.log("The word \"JavaScript\" is in the string.");
} else {
    console.log("The word \"JavaScript\" is not in the string.");
}

Output:-

The word “JavaScript” is in the string.

Check if String Contains Letters in JavaScript Using indexOf()

Like includes(), the JavaScript indexOf() method determines whether a string contains another string. The results of these two functions are different.

The includes() method produces a boolean, either true or false when we use it. The starting index position of the substring is returned by indexOf(). Alternatively, we’ll get “-1” if the string doesn’t include the substring.

Let’s examine this method’s syntax:-
string.indexOf(word);

let example = "JavaScript String!";
let substr = "JavaScript";

if (example.indexOf(substr) != -1) {
    console.log("The word \"" + substr + "\" is in the string.");
} else {
    console.log("The word \"" + substr + "\" is not in the string.");
}

substr = "Hello";

if (example.indexOf(substr) != -1) {
    console.log("The word \"" + substr + "\" is in the string.");
} else {
    console.log("The word \"" + substr + "\" is not in the string.");
}

Output:-

The word “JavaScript” is in the string.
The word “Hello” is not in the string.

Check if String Contains Letters in JavaScript using Regular expressions (regex)

Let’s examine this method’s syntax:

let str = "JavaScript String!";
console.log(/JavaScript/.test(str));
console.log(/Hello/.test(str));
console.log(/String/.test(str));
console.log(/Str/.test(str));

Output:-

true
false
true
true

We covered the fundamentals of strings in JavaScript in this tutorial. Following that, we went through three different ways you may use JavaScript to determine whether a text contains a substring: employing regex, indexOf(), and includes().

Perhaps the most used method for determining whether a string contains a substring is the includes() method. This is true because the method’s name is literal. It is obvious that includes() enable string searching within another string.

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

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