Search a string for a specific character in JavaScript
This post will discuss how to search a string for a specific character in JavaScript.
There are several ways to check a string for a specific character in JavaScript. Some of the common functions are:
1. Using includes() function
The includes() function is a modern and simple way of checking for a character or a substring in a string. This function returns true if the string contains the character, and false if it doesn’t. It is case-sensitive and accepts an optional second argument for the starting index. For example, to check if the string "Hello World" contains the character "r", we can use:
|
1 2 3 4 5 6 7 8 9 10 11 |
// A string variable let str = "Hello World"; // A character variable let char = "r"; // Check if the string contains the character let result = str.includes(char); // Prints true console.log(result); |
2. Using indexOf() function
The indexOf() function is a widely supported and versatile way of checking for a character or a substring in a string. This function returns the index of the first occurrence of the character in the string, or -1 if it is not found. It is also case-sensitive and accepts an optional second argument for the starting index. For example, using the same string and character as before, we can use:
|
1 2 3 4 5 6 7 8 9 10 11 |
// A string variable let str = "Hello World"; // A character variable let char = "r"; // Get the index of the character in the string let result = str.indexOf(char); // Prints 4 console.log(result); |
If we only want to check for the existence of the character, we can compare the result with the return value of the indexOf() function (str.indexOf(char) !== -1 or str.indexOf(char) >= 0). Here’s an example:
|
1 2 3 4 5 6 7 8 9 10 11 |
let str = "Hello World"; let char = "r"; // Check if the index is greater than or equal to 0 if (str.indexOf(char) >= 0) { console.log("The string contains the character"); } else { console.log("The string does not contain the character"); } // Prints "The string contains the character" |
3. Using match() or test() function
The match() function with a regular expression is a powerful and flexible way of checking for a characters or patterns in a string. This function returns an array of matches if the string matches the regular expression, or null if it does not. If we only want to check for the existence of the character, we can convert the result to a boolean value. For example, using the inputs as before, we can use:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
let str = "Hello World"; // Match the character "r" with a regular expression let result = str.match(/r/); // Prints ["r", index: 8, input: "Hello World", groups: undefined] console.log(result); // Check if the match is truthy if (result) { console.log("The string contains the character"); } else { console.log("The string does not contain the character"); } |
Alternatively, we can use the test() function to match a string against a regular expression and return a boolean value accordingly. It can also be used to check for specific patterns or characters in the string. Here’s an example:
|
1 2 3 4 5 6 7 8 |
let str = "Hello World"; // Check if the match is truthy if (/r/.test(str)) { console.log("The string contains the character"); } else { console.log("The string does not contain the character"); } |
That’s all about searching a string for a specific character in JavaScript.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)