Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

83
Views
Effectively Search for set of substrings in a string Javascript

I currently have a string and set of substrings/search strings which I want to effectively search in the given string. This is what I have currently:

const apple = "apple"
const banana = "banana"
const chickoo = "chickoo"
const dates = "dates"
const eggplant = "eggplant"
const default = "default"

let string = "foobar" // This String changes dynamically
if (string.includes(apple)){
    return apple;
} else if (string.includes(banana)) {
    return banana;
} else if (string.includes(chickoo)) {
    return chickoo;
} else if (string.includes(dates)) {
    return dates;
} else if (string.includes(eggplant)) {
    return eggplant;
} else {
    return default;
}

This approach works, however I am looking for a more compact and efficent way of searching for substrings in a given string.

Edit: I am currently using the following way:

const fruits = ["apple", "banana", "chickoo", "dates", "eggplant"];
let string = "foobar" //This is dynamic
for(let fruit in fruits) {
    if(string.includes(fruits[fruit])){
        return fruits[fruit];
    }
}
return "default";

Let me know if there is even more effective way to do this than the above one.

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

Using Regex:

function matchFruit(input) {
    const match = input.match(/apple|banana|chickoo|dates|eggplant|default/);
    if(match)
        return match[0];
    return "default";
}

Note

this will only return the first matching fruit in the string. So "foobarapplebanana" will return "apple". If instead you want to return an array of strings, [ "apple", "banana" ], then return match instead of match[0].

Test Here

about 4 years ago · Juan Pablo Isaza Report

0

You can check these includes in one line.

First you have to create the available list and then check it. If it was not found in the list, default is returned.

const apple = "apple"
const banana = "banana"
const chickoo = "chickoo"
const dates = "dates"
const eggplant = "eggplant"
const default = "default"

let string = "foobar" // This String changes dynamically

const availableFruits = [apple, banana, chickoo, dates, egplant]

return availableFruits.includes(string) ? string : default;
about 4 years ago · Juan Pablo Isaza Report

0

You can use filter()

let string = "foobar" //This is dynamic
var matchingFruits =  fruits.filter(fruit=>string.includes(fruit))
return matchingFruit.length>0?matchingFruit[0]:"default"
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!