Check a string for any of the given prefixes in JavaScript
This post will discuss how to check a string for any of the given prefixes in JavaScript. In other words, determine if a string begins with any of the given prefixes in JavaScript.
There are several methods to check if a string starts with any of given prefixes in JavaScript. Some of the common functions are:
1. Using startsWith() function
The startsWith() function is the latest way of checking a prefix in a string. This function returns true if the string starts with one of the given prefixes, and false otherwise. We can use a loop or an array function to iterate over the prefixes and check each one. For example, to check if the string "Hello world" starts with any of the prefixes "He", "Hi", or "Ho", we can use:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
let str = "Hello world"; // A string variable let prefixes = ["He", "Hi", "Ho"]; // An array of prefixes // A boolean variable to store the result let result = false; // Loop over the prefixes for (let prefix of prefixes) { // Check if the string starts with the prefix if (str.startsWith(prefix)) { // Set the result to true and break the loop result = true; break; } } console.log(result); // true |
2. Using indexOf() function
The indexOf() function is common way of checking for a prefix in a string. It returns the index of the first occurrence of a substring in the string, or -1 if it is not found. We can use a loop or an array function to iterate over the prefixes and check if their index is 0. For example, using the same string and prefixes as before, we can do:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
let str = "Hello world"; // A string variable let prefixes = ["He", "Hi", "Ho"]; // An array of prefixes // A boolean variable to store the result let result = false; // Loop over the prefixes for (let prefix of prefixes) { // Check if the index of the prefix is 0 if (str.indexOf(prefix) == 0) { // Set the result to true and break the loop result = true; break; } } console.log(result); // true |
3. Using match() function
The match() function returns an array of matches if the string matches the regular expression, or null if it does not. We can use a regular expression that combines the prefixes with an OR operator (|) and anchors them to the start of the string (^). If we only want to check for the existence of a match, we can convert the result to a boolean value. For example, using the same string and prefixes as before, we can do:
|
1 2 3 4 5 6 7 8 9 10 11 12 |
// A string variable let str = "Hello world"; // A regular expression that matches any of the prefixes at the start of the string let regex = /^(He|Hi|Ho)/; // Check if there is a match if (str.match(regex)) { console.log("The string starts with one of the prefixes"); } else { console.log("The string does not start with any of the prefixes"); } |
That’s all about checking a string for any of the given prefixes 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 :)