6

In javascript, how can I check if a string starts with any of the strings in an array.

For example,

I have an array of strings,

const substrs = ['the', 'an', 'I'];

I have a string

const str = 'the car';

How can I check if str starts with any of the strings in substrs?

Akhil Chandran
  • 380
  • 3
  • 12

4 Answers4

21

You can use a combination of Array.prototype.some and String.prototype.startsWith, both of which are ES6 features:

const substrs = ['the', 'an', 'I'];
function checkIfStringStartsWith(str, substrs) {
  return substrs.some(substr => str.startsWith(substr));
}

console.log(checkIfStringStartsWith('the car', substrs)); // true
console.log(checkIfStringStartsWith('a car', substrs)); // false
console.log(checkIfStringStartsWith('i am a car', substrs));  // false
console.log(checkIfStringStartsWith('I am a car', substrs));  // true

If you want the comparsion to be done in a case-insensitive manner, then you will need to convert the string to lowercase as well:

const substrs = ['the', 'an', 'I'];
function checkIfStringStartsWith(str, substrs) {
  return substrs.some(substr => str.toLowerCase().startsWith(substr.toLowerCase()));
}

console.log(checkIfStringStartsWith('the car', substrs)); // true
console.log(checkIfStringStartsWith('a car', substrs)); // false
console.log(checkIfStringStartsWith('i am a car', substrs));  // true (case-insensitive)
console.log(checkIfStringStartsWith('I am a car', substrs));  // true
Terry
  • 63,248
  • 15
  • 96
  • 118
0

One approach would be to build a regex alternation of the keywords in the substrs array. Then, use test() on the input to check for a match:

const substrs = ['the', 'an', 'I'];
const str = 'the car';
regexp = new RegExp('^(?:' + substrs.join('|') + ')\\b');
console.log(regexp);
if (regexp.test(str)) {
    console.log("MATCH");
}

Here I have also printed out the regular expression being used so that it is clear.

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
  • 2
    Careful with this approach. This works only as long as the strings in the array don't contain any characters that have special meaning in a regular expression; most likely `.`, `+` and `*`. Then you need to start thinking about escaping things and it becomes an even more overengineered solution. – Thomas Jun 07 '21 at 06:26
  • @Thomas Escaping a regex in JS is fairly easy, [see here](https://stackoverflow.com/questions/3446170/escape-string-for-use-in-javascript-regex), in fact Node has a built in function for this. _You_ are the one overengineering the question, as we have no strong reason to believe that the keywords would be anything but word characters only. – Tim Biegeleisen Jun 07 '21 at 06:31
  • 2
    You just don't need regex for this. – Stefan Falk Jun 07 '21 at 11:14
0

const substrs = ['the', 'an', 'I'];
const str = 'the car';

let result = substrs.filter(s => str.startsWith(s));

console.log(result);
Vivek Bani
  • 3,703
  • 1
  • 9
  • 18
  • 1
    @jfriend00 No, Please reread mycode properly. `str.startsWith(s)` here what is str and what is s? – Vivek Bani Jun 07 '21 at 06:19
  • OP is is looking for sentences that start with words `I` , `the` , `an`. In your case `and`,`Interest`,`them` are also considered. – Akshay G Jun 07 '21 at 06:19
  • Man, that is one confusing way to write things. – jfriend00 Jun 07 '21 at 06:21
  • @AkshayGaonkar Read the question properly first – Vivek Bani Jun 07 '21 at 06:22
  • 1
    Well, you can be surprised at how dumb you think I am OR you can realize that maybe writing the code that uses methods in an unusual way might confuse people. It also seems wasteful to create a whole output array when all the OP wants is a boolean. I didn't downvote you - I just was confused by this code the first time I saw it and I shared that in a comment and I think that's a relevant comment to answers here. Also, telling people to reread something RATHER than just explaining in words how your answer works isn't the best way to clarify your answer or clear up confusion or earn votes. – jfriend00 Jun 07 '21 at 06:34
  • 2
    Posting code without explanation is rarely helpful. – Felix Kling Jun 07 '21 at 09:37
0
const check = (arr, str) => {
    const firstWord = str.substr(0, str.indexOf(" "));
    return arr.includes(firstWord);
}

const substrs = ['thee', 'an', 'I', 'The', 'the'];
const str = 'the car';

console.log(check(substrs, str)); //true
Reza
  • 493
  • 5
  • 14