jquery/javascript check string for multiple substrings











up vote
4
down vote

favorite
4












I need to check if a string has one of three substrings, and if yes, to implement a function. I know I can check for one substring using if (str.indexOf("term1") >= 0) but is there a way to check for multiple substrings short of using several instances of this code?



TIA










share|improve this question
























  • Put it in a loop?
    – musefan
    Mar 4 '13 at 12:46






  • 1




    if (str.indexOf("term1") >= 0 || str.indexOf("term2") >= 0 || str.indexOf("term3") >= 0) How difficult could it be?
    – Alvin Wong
    Mar 4 '13 at 12:48

















up vote
4
down vote

favorite
4












I need to check if a string has one of three substrings, and if yes, to implement a function. I know I can check for one substring using if (str.indexOf("term1") >= 0) but is there a way to check for multiple substrings short of using several instances of this code?



TIA










share|improve this question
























  • Put it in a loop?
    – musefan
    Mar 4 '13 at 12:46






  • 1




    if (str.indexOf("term1") >= 0 || str.indexOf("term2") >= 0 || str.indexOf("term3") >= 0) How difficult could it be?
    – Alvin Wong
    Mar 4 '13 at 12:48















up vote
4
down vote

favorite
4









up vote
4
down vote

favorite
4






4





I need to check if a string has one of three substrings, and if yes, to implement a function. I know I can check for one substring using if (str.indexOf("term1") >= 0) but is there a way to check for multiple substrings short of using several instances of this code?



TIA










share|improve this question















I need to check if a string has one of three substrings, and if yes, to implement a function. I know I can check for one substring using if (str.indexOf("term1") >= 0) but is there a way to check for multiple substrings short of using several instances of this code?



TIA







javascript jquery string search






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 8 at 8:54









Dem Pilafian

2,73612548




2,73612548










asked Mar 4 '13 at 12:46









Phil

67451430




67451430












  • Put it in a loop?
    – musefan
    Mar 4 '13 at 12:46






  • 1




    if (str.indexOf("term1") >= 0 || str.indexOf("term2") >= 0 || str.indexOf("term3") >= 0) How difficult could it be?
    – Alvin Wong
    Mar 4 '13 at 12:48




















  • Put it in a loop?
    – musefan
    Mar 4 '13 at 12:46






  • 1




    if (str.indexOf("term1") >= 0 || str.indexOf("term2") >= 0 || str.indexOf("term3") >= 0) How difficult could it be?
    – Alvin Wong
    Mar 4 '13 at 12:48


















Put it in a loop?
– musefan
Mar 4 '13 at 12:46




Put it in a loop?
– musefan
Mar 4 '13 at 12:46




1




1




if (str.indexOf("term1") >= 0 || str.indexOf("term2") >= 0 || str.indexOf("term3") >= 0) How difficult could it be?
– Alvin Wong
Mar 4 '13 at 12:48






if (str.indexOf("term1") >= 0 || str.indexOf("term2") >= 0 || str.indexOf("term3") >= 0) How difficult could it be?
– Alvin Wong
Mar 4 '13 at 12:48














5 Answers
5






active

oldest

votes

















up vote
7
down vote













You could use a loop. Maybe even create a helper function like so:



function ContainsAny(str, items){
for(var i in items){
var item = items[i];
if (str.indexOf(item) > -1){
return true;
}

}
return false;
}


Which you can then call like so:



if(ContainsAny(str, ["term1", "term2", "term3"])){
//do something
}





share|improve this answer























  • From the question: "I need to check if a string has one of three substrings" It's not a requirement for all of them to be present, only at least one of them.
    – Anthony Grist
    Mar 4 '13 at 12:56










  • @AnthonyGrist: Yeah I seen that already and just changed it
    – musefan
    Mar 4 '13 at 12:57




















up vote
7
down vote













if (/term1|term2|term3/.test("your string")) {
//youre code
}





share|improve this answer



















  • 1




    Explanation of downvote would be nice.
    – elrado
    Mar 4 '13 at 12:56










  • You don't have to explain downvotes but I'd guess that not everyone likes Regexes. In this situation they aren't needed and some may say they make the code less readable.
    – Ash Burlaczenko
    Mar 4 '13 at 13:01






  • 4




    Buth they're simple and elegant. Why would I want to use loong OR (||) clause or loops If I know my regex.
    – elrado
    Mar 4 '13 at 13:06












  • explanation of the answer is missing why would you ask for explanation of downvote?Dont ask for something you didnt give.
    – guradio
    Nov 8 at 8:58




















up vote
3
down vote













Maybe this:



if (str.indexOf("term1") >= 0 || str.indexOf("term2") >= 0 || str.indexOf("term3") >= 0) 
{
//your code
}





share|improve this answer





















  • +1 for the clear simple solution !
    – Hussein Nazzal
    Mar 4 '13 at 12:51






  • 1




    Not ideal for an unknown number of terms though
    – musefan
    Mar 4 '13 at 12:55






  • 1




    I agree, but he asked for 3 substrings.
    – freshbm
    Mar 4 '13 at 13:00






  • 2




    but he asked short of using several instances of this code
    – Arun P Johny
    Mar 4 '13 at 13:16


















up vote
2
down vote













You can do something like



function isSubStringPresent(str){
for(var i = 1; i < arguments.length; i++){
if(str.indexOf(arguments[i]) > -1){
return true;
}
}

return false;
}

isSubStringPresent('mystring', 'term1', 'term2', ...)





share|improve this answer























  • Never seen this before, using extra arguments. Can you point me to the docs?
    – Ash Burlaczenko
    Mar 4 '13 at 12:53










  • Interesting, I never knew about arguments before... though I assume this will be vulnerable to redefinition?
    – musefan
    Mar 4 '13 at 12:54












  • every javascript method get a hidden parameter called arguments which is an array like structure with all the parameters passed to the method. It is like variable arguments in java
    – Arun P Johny
    Mar 4 '13 at 12:55










  • no, it is as per standard ECMA-262
    – Arun P Johny
    Mar 4 '13 at 12:58






  • 2




    On the other hand, isn't you snippet wrong? You will exit with false if any of the substrings are not in the string. Doesn't the OP want to check that at least one is in the string?
    – Ash Burlaczenko
    Mar 4 '13 at 13:06


















up vote
0
down vote













The .map() function can be used to convert an array of terms into an array of booleans indicating if each term is found. Then check if any of the booleans are true.



Given an array of terms:



const terms = ['term1', 'term2', 'term3'];


This line of code will return true if string contains any of the terms:



terms.map((term) => string.includes(term)).includes(true);       


Three examples:



terms.map((term) => 'Got term2 here'.includes(term)).includes(true);       //true
terms.map((term) => 'Not here'.includes(term)).includes(true); //false
terms.map((term) => 'Got term1 and term3'.includes(term)).includes(true); //true


Or, if you want to wrap the code up into a reusable hasTerm() function:



function hasTerm(string, terms) {
function search(term) { return string.includes(term); }
return terms.map(search).includes(true);
}
hasTerm('Got term2 here', terms); //true
hasTerm('Not here', terms); //false
hasTerm('Got term1 and term3', terms); //true


Try it out:
https://codepen.io/anon/pen/MzKZZQ?editors=0012



.map() documentation:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map



Notes:




  1. This answer optimizes for simplicity and readability. If extremely large arrays of terms are expected, use a loop that short-circuits once a term is found.

  2. To support IE, transpile to replace occurrences of .includes(x) with .indexOf(x) !== -1 and => with a function declaration.






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














     

    draft saved


    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f15201939%2fjquery-javascript-check-string-for-multiple-substrings%23new-answer', 'question_page');
    }
    );

    Post as a guest
































    5 Answers
    5






    active

    oldest

    votes








    5 Answers
    5






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    7
    down vote













    You could use a loop. Maybe even create a helper function like so:



    function ContainsAny(str, items){
    for(var i in items){
    var item = items[i];
    if (str.indexOf(item) > -1){
    return true;
    }

    }
    return false;
    }


    Which you can then call like so:



    if(ContainsAny(str, ["term1", "term2", "term3"])){
    //do something
    }





    share|improve this answer























    • From the question: "I need to check if a string has one of three substrings" It's not a requirement for all of them to be present, only at least one of them.
      – Anthony Grist
      Mar 4 '13 at 12:56










    • @AnthonyGrist: Yeah I seen that already and just changed it
      – musefan
      Mar 4 '13 at 12:57

















    up vote
    7
    down vote













    You could use a loop. Maybe even create a helper function like so:



    function ContainsAny(str, items){
    for(var i in items){
    var item = items[i];
    if (str.indexOf(item) > -1){
    return true;
    }

    }
    return false;
    }


    Which you can then call like so:



    if(ContainsAny(str, ["term1", "term2", "term3"])){
    //do something
    }





    share|improve this answer























    • From the question: "I need to check if a string has one of three substrings" It's not a requirement for all of them to be present, only at least one of them.
      – Anthony Grist
      Mar 4 '13 at 12:56










    • @AnthonyGrist: Yeah I seen that already and just changed it
      – musefan
      Mar 4 '13 at 12:57















    up vote
    7
    down vote










    up vote
    7
    down vote









    You could use a loop. Maybe even create a helper function like so:



    function ContainsAny(str, items){
    for(var i in items){
    var item = items[i];
    if (str.indexOf(item) > -1){
    return true;
    }

    }
    return false;
    }


    Which you can then call like so:



    if(ContainsAny(str, ["term1", "term2", "term3"])){
    //do something
    }





    share|improve this answer














    You could use a loop. Maybe even create a helper function like so:



    function ContainsAny(str, items){
    for(var i in items){
    var item = items[i];
    if (str.indexOf(item) > -1){
    return true;
    }

    }
    return false;
    }


    Which you can then call like so:



    if(ContainsAny(str, ["term1", "term2", "term3"])){
    //do something
    }






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Jan 3 '14 at 15:46

























    answered Mar 4 '13 at 12:49









    musefan

    39.4k1696148




    39.4k1696148












    • From the question: "I need to check if a string has one of three substrings" It's not a requirement for all of them to be present, only at least one of them.
      – Anthony Grist
      Mar 4 '13 at 12:56










    • @AnthonyGrist: Yeah I seen that already and just changed it
      – musefan
      Mar 4 '13 at 12:57




















    • From the question: "I need to check if a string has one of three substrings" It's not a requirement for all of them to be present, only at least one of them.
      – Anthony Grist
      Mar 4 '13 at 12:56










    • @AnthonyGrist: Yeah I seen that already and just changed it
      – musefan
      Mar 4 '13 at 12:57


















    From the question: "I need to check if a string has one of three substrings" It's not a requirement for all of them to be present, only at least one of them.
    – Anthony Grist
    Mar 4 '13 at 12:56




    From the question: "I need to check if a string has one of three substrings" It's not a requirement for all of them to be present, only at least one of them.
    – Anthony Grist
    Mar 4 '13 at 12:56












    @AnthonyGrist: Yeah I seen that already and just changed it
    – musefan
    Mar 4 '13 at 12:57






    @AnthonyGrist: Yeah I seen that already and just changed it
    – musefan
    Mar 4 '13 at 12:57














    up vote
    7
    down vote













    if (/term1|term2|term3/.test("your string")) {
    //youre code
    }





    share|improve this answer



















    • 1




      Explanation of downvote would be nice.
      – elrado
      Mar 4 '13 at 12:56










    • You don't have to explain downvotes but I'd guess that not everyone likes Regexes. In this situation they aren't needed and some may say they make the code less readable.
      – Ash Burlaczenko
      Mar 4 '13 at 13:01






    • 4




      Buth they're simple and elegant. Why would I want to use loong OR (||) clause or loops If I know my regex.
      – elrado
      Mar 4 '13 at 13:06












    • explanation of the answer is missing why would you ask for explanation of downvote?Dont ask for something you didnt give.
      – guradio
      Nov 8 at 8:58

















    up vote
    7
    down vote













    if (/term1|term2|term3/.test("your string")) {
    //youre code
    }





    share|improve this answer



















    • 1




      Explanation of downvote would be nice.
      – elrado
      Mar 4 '13 at 12:56










    • You don't have to explain downvotes but I'd guess that not everyone likes Regexes. In this situation they aren't needed and some may say they make the code less readable.
      – Ash Burlaczenko
      Mar 4 '13 at 13:01






    • 4




      Buth they're simple and elegant. Why would I want to use loong OR (||) clause or loops If I know my regex.
      – elrado
      Mar 4 '13 at 13:06












    • explanation of the answer is missing why would you ask for explanation of downvote?Dont ask for something you didnt give.
      – guradio
      Nov 8 at 8:58















    up vote
    7
    down vote










    up vote
    7
    down vote









    if (/term1|term2|term3/.test("your string")) {
    //youre code
    }





    share|improve this answer














    if (/term1|term2|term3/.test("your string")) {
    //youre code
    }






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Jul 13 '15 at 13:22

























    answered Mar 4 '13 at 12:49









    elrado

    2,4511011




    2,4511011








    • 1




      Explanation of downvote would be nice.
      – elrado
      Mar 4 '13 at 12:56










    • You don't have to explain downvotes but I'd guess that not everyone likes Regexes. In this situation they aren't needed and some may say they make the code less readable.
      – Ash Burlaczenko
      Mar 4 '13 at 13:01






    • 4




      Buth they're simple and elegant. Why would I want to use loong OR (||) clause or loops If I know my regex.
      – elrado
      Mar 4 '13 at 13:06












    • explanation of the answer is missing why would you ask for explanation of downvote?Dont ask for something you didnt give.
      – guradio
      Nov 8 at 8:58
















    • 1




      Explanation of downvote would be nice.
      – elrado
      Mar 4 '13 at 12:56










    • You don't have to explain downvotes but I'd guess that not everyone likes Regexes. In this situation they aren't needed and some may say they make the code less readable.
      – Ash Burlaczenko
      Mar 4 '13 at 13:01






    • 4




      Buth they're simple and elegant. Why would I want to use loong OR (||) clause or loops If I know my regex.
      – elrado
      Mar 4 '13 at 13:06












    • explanation of the answer is missing why would you ask for explanation of downvote?Dont ask for something you didnt give.
      – guradio
      Nov 8 at 8:58










    1




    1




    Explanation of downvote would be nice.
    – elrado
    Mar 4 '13 at 12:56




    Explanation of downvote would be nice.
    – elrado
    Mar 4 '13 at 12:56












    You don't have to explain downvotes but I'd guess that not everyone likes Regexes. In this situation they aren't needed and some may say they make the code less readable.
    – Ash Burlaczenko
    Mar 4 '13 at 13:01




    You don't have to explain downvotes but I'd guess that not everyone likes Regexes. In this situation they aren't needed and some may say they make the code less readable.
    – Ash Burlaczenko
    Mar 4 '13 at 13:01




    4




    4




    Buth they're simple and elegant. Why would I want to use loong OR (||) clause or loops If I know my regex.
    – elrado
    Mar 4 '13 at 13:06






    Buth they're simple and elegant. Why would I want to use loong OR (||) clause or loops If I know my regex.
    – elrado
    Mar 4 '13 at 13:06














    explanation of the answer is missing why would you ask for explanation of downvote?Dont ask for something you didnt give.
    – guradio
    Nov 8 at 8:58






    explanation of the answer is missing why would you ask for explanation of downvote?Dont ask for something you didnt give.
    – guradio
    Nov 8 at 8:58












    up vote
    3
    down vote













    Maybe this:



    if (str.indexOf("term1") >= 0 || str.indexOf("term2") >= 0 || str.indexOf("term3") >= 0) 
    {
    //your code
    }





    share|improve this answer





















    • +1 for the clear simple solution !
      – Hussein Nazzal
      Mar 4 '13 at 12:51






    • 1




      Not ideal for an unknown number of terms though
      – musefan
      Mar 4 '13 at 12:55






    • 1




      I agree, but he asked for 3 substrings.
      – freshbm
      Mar 4 '13 at 13:00






    • 2




      but he asked short of using several instances of this code
      – Arun P Johny
      Mar 4 '13 at 13:16















    up vote
    3
    down vote













    Maybe this:



    if (str.indexOf("term1") >= 0 || str.indexOf("term2") >= 0 || str.indexOf("term3") >= 0) 
    {
    //your code
    }





    share|improve this answer





















    • +1 for the clear simple solution !
      – Hussein Nazzal
      Mar 4 '13 at 12:51






    • 1




      Not ideal for an unknown number of terms though
      – musefan
      Mar 4 '13 at 12:55






    • 1




      I agree, but he asked for 3 substrings.
      – freshbm
      Mar 4 '13 at 13:00






    • 2




      but he asked short of using several instances of this code
      – Arun P Johny
      Mar 4 '13 at 13:16













    up vote
    3
    down vote










    up vote
    3
    down vote









    Maybe this:



    if (str.indexOf("term1") >= 0 || str.indexOf("term2") >= 0 || str.indexOf("term3") >= 0) 
    {
    //your code
    }





    share|improve this answer












    Maybe this:



    if (str.indexOf("term1") >= 0 || str.indexOf("term2") >= 0 || str.indexOf("term3") >= 0) 
    {
    //your code
    }






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Mar 4 '13 at 12:48









    freshbm

    4,72023262




    4,72023262












    • +1 for the clear simple solution !
      – Hussein Nazzal
      Mar 4 '13 at 12:51






    • 1




      Not ideal for an unknown number of terms though
      – musefan
      Mar 4 '13 at 12:55






    • 1




      I agree, but he asked for 3 substrings.
      – freshbm
      Mar 4 '13 at 13:00






    • 2




      but he asked short of using several instances of this code
      – Arun P Johny
      Mar 4 '13 at 13:16


















    • +1 for the clear simple solution !
      – Hussein Nazzal
      Mar 4 '13 at 12:51






    • 1




      Not ideal for an unknown number of terms though
      – musefan
      Mar 4 '13 at 12:55






    • 1




      I agree, but he asked for 3 substrings.
      – freshbm
      Mar 4 '13 at 13:00






    • 2




      but he asked short of using several instances of this code
      – Arun P Johny
      Mar 4 '13 at 13:16
















    +1 for the clear simple solution !
    – Hussein Nazzal
    Mar 4 '13 at 12:51




    +1 for the clear simple solution !
    – Hussein Nazzal
    Mar 4 '13 at 12:51




    1




    1




    Not ideal for an unknown number of terms though
    – musefan
    Mar 4 '13 at 12:55




    Not ideal for an unknown number of terms though
    – musefan
    Mar 4 '13 at 12:55




    1




    1




    I agree, but he asked for 3 substrings.
    – freshbm
    Mar 4 '13 at 13:00




    I agree, but he asked for 3 substrings.
    – freshbm
    Mar 4 '13 at 13:00




    2




    2




    but he asked short of using several instances of this code
    – Arun P Johny
    Mar 4 '13 at 13:16




    but he asked short of using several instances of this code
    – Arun P Johny
    Mar 4 '13 at 13:16










    up vote
    2
    down vote













    You can do something like



    function isSubStringPresent(str){
    for(var i = 1; i < arguments.length; i++){
    if(str.indexOf(arguments[i]) > -1){
    return true;
    }
    }

    return false;
    }

    isSubStringPresent('mystring', 'term1', 'term2', ...)





    share|improve this answer























    • Never seen this before, using extra arguments. Can you point me to the docs?
      – Ash Burlaczenko
      Mar 4 '13 at 12:53










    • Interesting, I never knew about arguments before... though I assume this will be vulnerable to redefinition?
      – musefan
      Mar 4 '13 at 12:54












    • every javascript method get a hidden parameter called arguments which is an array like structure with all the parameters passed to the method. It is like variable arguments in java
      – Arun P Johny
      Mar 4 '13 at 12:55










    • no, it is as per standard ECMA-262
      – Arun P Johny
      Mar 4 '13 at 12:58






    • 2




      On the other hand, isn't you snippet wrong? You will exit with false if any of the substrings are not in the string. Doesn't the OP want to check that at least one is in the string?
      – Ash Burlaczenko
      Mar 4 '13 at 13:06















    up vote
    2
    down vote













    You can do something like



    function isSubStringPresent(str){
    for(var i = 1; i < arguments.length; i++){
    if(str.indexOf(arguments[i]) > -1){
    return true;
    }
    }

    return false;
    }

    isSubStringPresent('mystring', 'term1', 'term2', ...)





    share|improve this answer























    • Never seen this before, using extra arguments. Can you point me to the docs?
      – Ash Burlaczenko
      Mar 4 '13 at 12:53










    • Interesting, I never knew about arguments before... though I assume this will be vulnerable to redefinition?
      – musefan
      Mar 4 '13 at 12:54












    • every javascript method get a hidden parameter called arguments which is an array like structure with all the parameters passed to the method. It is like variable arguments in java
      – Arun P Johny
      Mar 4 '13 at 12:55










    • no, it is as per standard ECMA-262
      – Arun P Johny
      Mar 4 '13 at 12:58






    • 2




      On the other hand, isn't you snippet wrong? You will exit with false if any of the substrings are not in the string. Doesn't the OP want to check that at least one is in the string?
      – Ash Burlaczenko
      Mar 4 '13 at 13:06













    up vote
    2
    down vote










    up vote
    2
    down vote









    You can do something like



    function isSubStringPresent(str){
    for(var i = 1; i < arguments.length; i++){
    if(str.indexOf(arguments[i]) > -1){
    return true;
    }
    }

    return false;
    }

    isSubStringPresent('mystring', 'term1', 'term2', ...)





    share|improve this answer














    You can do something like



    function isSubStringPresent(str){
    for(var i = 1; i < arguments.length; i++){
    if(str.indexOf(arguments[i]) > -1){
    return true;
    }
    }

    return false;
    }

    isSubStringPresent('mystring', 'term1', 'term2', ...)






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 8 at 8:56









    guradio

    13.7k31836




    13.7k31836










    answered Mar 4 '13 at 12:50









    Arun P Johny

    319k49421441




    319k49421441












    • Never seen this before, using extra arguments. Can you point me to the docs?
      – Ash Burlaczenko
      Mar 4 '13 at 12:53










    • Interesting, I never knew about arguments before... though I assume this will be vulnerable to redefinition?
      – musefan
      Mar 4 '13 at 12:54












    • every javascript method get a hidden parameter called arguments which is an array like structure with all the parameters passed to the method. It is like variable arguments in java
      – Arun P Johny
      Mar 4 '13 at 12:55










    • no, it is as per standard ECMA-262
      – Arun P Johny
      Mar 4 '13 at 12:58






    • 2




      On the other hand, isn't you snippet wrong? You will exit with false if any of the substrings are not in the string. Doesn't the OP want to check that at least one is in the string?
      – Ash Burlaczenko
      Mar 4 '13 at 13:06


















    • Never seen this before, using extra arguments. Can you point me to the docs?
      – Ash Burlaczenko
      Mar 4 '13 at 12:53










    • Interesting, I never knew about arguments before... though I assume this will be vulnerable to redefinition?
      – musefan
      Mar 4 '13 at 12:54












    • every javascript method get a hidden parameter called arguments which is an array like structure with all the parameters passed to the method. It is like variable arguments in java
      – Arun P Johny
      Mar 4 '13 at 12:55










    • no, it is as per standard ECMA-262
      – Arun P Johny
      Mar 4 '13 at 12:58






    • 2




      On the other hand, isn't you snippet wrong? You will exit with false if any of the substrings are not in the string. Doesn't the OP want to check that at least one is in the string?
      – Ash Burlaczenko
      Mar 4 '13 at 13:06
















    Never seen this before, using extra arguments. Can you point me to the docs?
    – Ash Burlaczenko
    Mar 4 '13 at 12:53




    Never seen this before, using extra arguments. Can you point me to the docs?
    – Ash Burlaczenko
    Mar 4 '13 at 12:53












    Interesting, I never knew about arguments before... though I assume this will be vulnerable to redefinition?
    – musefan
    Mar 4 '13 at 12:54






    Interesting, I never knew about arguments before... though I assume this will be vulnerable to redefinition?
    – musefan
    Mar 4 '13 at 12:54














    every javascript method get a hidden parameter called arguments which is an array like structure with all the parameters passed to the method. It is like variable arguments in java
    – Arun P Johny
    Mar 4 '13 at 12:55




    every javascript method get a hidden parameter called arguments which is an array like structure with all the parameters passed to the method. It is like variable arguments in java
    – Arun P Johny
    Mar 4 '13 at 12:55












    no, it is as per standard ECMA-262
    – Arun P Johny
    Mar 4 '13 at 12:58




    no, it is as per standard ECMA-262
    – Arun P Johny
    Mar 4 '13 at 12:58




    2




    2




    On the other hand, isn't you snippet wrong? You will exit with false if any of the substrings are not in the string. Doesn't the OP want to check that at least one is in the string?
    – Ash Burlaczenko
    Mar 4 '13 at 13:06




    On the other hand, isn't you snippet wrong? You will exit with false if any of the substrings are not in the string. Doesn't the OP want to check that at least one is in the string?
    – Ash Burlaczenko
    Mar 4 '13 at 13:06










    up vote
    0
    down vote













    The .map() function can be used to convert an array of terms into an array of booleans indicating if each term is found. Then check if any of the booleans are true.



    Given an array of terms:



    const terms = ['term1', 'term2', 'term3'];


    This line of code will return true if string contains any of the terms:



    terms.map((term) => string.includes(term)).includes(true);       


    Three examples:



    terms.map((term) => 'Got term2 here'.includes(term)).includes(true);       //true
    terms.map((term) => 'Not here'.includes(term)).includes(true); //false
    terms.map((term) => 'Got term1 and term3'.includes(term)).includes(true); //true


    Or, if you want to wrap the code up into a reusable hasTerm() function:



    function hasTerm(string, terms) {
    function search(term) { return string.includes(term); }
    return terms.map(search).includes(true);
    }
    hasTerm('Got term2 here', terms); //true
    hasTerm('Not here', terms); //false
    hasTerm('Got term1 and term3', terms); //true


    Try it out:
    https://codepen.io/anon/pen/MzKZZQ?editors=0012



    .map() documentation:
    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map



    Notes:




    1. This answer optimizes for simplicity and readability. If extremely large arrays of terms are expected, use a loop that short-circuits once a term is found.

    2. To support IE, transpile to replace occurrences of .includes(x) with .indexOf(x) !== -1 and => with a function declaration.






    share|improve this answer



























      up vote
      0
      down vote













      The .map() function can be used to convert an array of terms into an array of booleans indicating if each term is found. Then check if any of the booleans are true.



      Given an array of terms:



      const terms = ['term1', 'term2', 'term3'];


      This line of code will return true if string contains any of the terms:



      terms.map((term) => string.includes(term)).includes(true);       


      Three examples:



      terms.map((term) => 'Got term2 here'.includes(term)).includes(true);       //true
      terms.map((term) => 'Not here'.includes(term)).includes(true); //false
      terms.map((term) => 'Got term1 and term3'.includes(term)).includes(true); //true


      Or, if you want to wrap the code up into a reusable hasTerm() function:



      function hasTerm(string, terms) {
      function search(term) { return string.includes(term); }
      return terms.map(search).includes(true);
      }
      hasTerm('Got term2 here', terms); //true
      hasTerm('Not here', terms); //false
      hasTerm('Got term1 and term3', terms); //true


      Try it out:
      https://codepen.io/anon/pen/MzKZZQ?editors=0012



      .map() documentation:
      https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map



      Notes:




      1. This answer optimizes for simplicity and readability. If extremely large arrays of terms are expected, use a loop that short-circuits once a term is found.

      2. To support IE, transpile to replace occurrences of .includes(x) with .indexOf(x) !== -1 and => with a function declaration.






      share|improve this answer

























        up vote
        0
        down vote










        up vote
        0
        down vote









        The .map() function can be used to convert an array of terms into an array of booleans indicating if each term is found. Then check if any of the booleans are true.



        Given an array of terms:



        const terms = ['term1', 'term2', 'term3'];


        This line of code will return true if string contains any of the terms:



        terms.map((term) => string.includes(term)).includes(true);       


        Three examples:



        terms.map((term) => 'Got term2 here'.includes(term)).includes(true);       //true
        terms.map((term) => 'Not here'.includes(term)).includes(true); //false
        terms.map((term) => 'Got term1 and term3'.includes(term)).includes(true); //true


        Or, if you want to wrap the code up into a reusable hasTerm() function:



        function hasTerm(string, terms) {
        function search(term) { return string.includes(term); }
        return terms.map(search).includes(true);
        }
        hasTerm('Got term2 here', terms); //true
        hasTerm('Not here', terms); //false
        hasTerm('Got term1 and term3', terms); //true


        Try it out:
        https://codepen.io/anon/pen/MzKZZQ?editors=0012



        .map() documentation:
        https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map



        Notes:




        1. This answer optimizes for simplicity and readability. If extremely large arrays of terms are expected, use a loop that short-circuits once a term is found.

        2. To support IE, transpile to replace occurrences of .includes(x) with .indexOf(x) !== -1 and => with a function declaration.






        share|improve this answer














        The .map() function can be used to convert an array of terms into an array of booleans indicating if each term is found. Then check if any of the booleans are true.



        Given an array of terms:



        const terms = ['term1', 'term2', 'term3'];


        This line of code will return true if string contains any of the terms:



        terms.map((term) => string.includes(term)).includes(true);       


        Three examples:



        terms.map((term) => 'Got term2 here'.includes(term)).includes(true);       //true
        terms.map((term) => 'Not here'.includes(term)).includes(true); //false
        terms.map((term) => 'Got term1 and term3'.includes(term)).includes(true); //true


        Or, if you want to wrap the code up into a reusable hasTerm() function:



        function hasTerm(string, terms) {
        function search(term) { return string.includes(term); }
        return terms.map(search).includes(true);
        }
        hasTerm('Got term2 here', terms); //true
        hasTerm('Not here', terms); //false
        hasTerm('Got term1 and term3', terms); //true


        Try it out:
        https://codepen.io/anon/pen/MzKZZQ?editors=0012



        .map() documentation:
        https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map



        Notes:




        1. This answer optimizes for simplicity and readability. If extremely large arrays of terms are expected, use a loop that short-circuits once a term is found.

        2. To support IE, transpile to replace occurrences of .includes(x) with .indexOf(x) !== -1 and => with a function declaration.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 8 at 8:50

























        answered Nov 8 at 8:26









        Dem Pilafian

        2,73612548




        2,73612548






























             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f15201939%2fjquery-javascript-check-string-for-multiple-substrings%23new-answer', 'question_page');
            }
            );

            Post as a guest




















































































            Popular posts from this blog

            Landwehr

            Reims

            Schenkenzell