JavaScript Look for keywords in a string in JS

I have an input field where I expect users to type text that contains 1 of many keywords that will trigger different audio files depending on the keyword. (I know that's not really smart from UX point of view but that's just a mockup/demo of a virtual assistant).

I'm using this code but I feel I can do much better, can you suggest some alternatives?

    keyword1 = "music";
    keyword2 = "news";
    keyword3 = "weather";
    keyword4 = "cooking";
    keyword5 = "pasta";
    keyword6 = "tech";

    if(text.search(keyword1)!=-1) {
      audio.src = a_music;
      audio.play();
    } else if(text.search(keyword2)!=-1){
      audio.src = a_news;
      audio.play();
    } 
   [...]
  }
Answer:1

You could create an object with the keyword as key and the file url as value and then iterate over the keys to check if the text matches a keyword.

const config = { 
  'music': 'musicUrl',
  'news': 'newsUrl',
  'weather': 'weatherUrl',
  'cooking': 'cookingUrl',
  'pasta': 'pastaUrl',
  'tech': 'techUrl'
};

function match(input, obj) {
  var matched = Object.keys(obj).find(key => input.toLowerCase().search(key) > -1);
  return obj[matched] || null;
}

console.log(match('cats weather dogs', config));
console.log(match('cats tech dogs', config));
console.log(match('cats dogs', config));
Answer:2

You should use an array of keywords instead of one different variable per word. Then it's a piece of cake :

const keywords = ["music","news","weather","cooking","pasta","tech"]

const text = "let's play some music"

if( keywords.some(keyword => text.includes(keyword) )) {
    console.log("Found")
      // audio.src = a_music;
      // audio.play();
}
Answer:3

Perhaps an array?

var array = ["music", "news", "weather", "cooking", "pasta", "tech"];

for(var i = 0; i < array.length; i++){
    if(text.includes(array[i])){
    //do something with i
    break;
  }
}
Answer:4

Using a regex here seems a nice approach to me

let keywords = {
  music: 'musicSample',
  news: 'newsSample',
  weather: 'weatherSample',
  cooking: 'cookingSample',
  pasta: 'pastaSample',
  tech: 'techSample'
}

function searchKeywords (text) {
  let keys = Object.keys(keywords)
  let re = new RegExp('(' + keys.join('|') + ')', 'g')
  
  return text.toLowerCase().match(re)
}

console.log(searchKeywords('I love music and tech'))

// Play the first encountered word or queue every word to play consecutively what you found
console.log(searchKeywords('Mama is cooking some pasta')[0])
Answer:5

I have written Backbone view - ListView and want to display table in to it. For individual table rows, there is another view - EmployeeView. I am binding click event on delete button which is inside ...

I have written Backbone view - ListView and want to display table in to it. For individual table rows, there is another view - EmployeeView. I am binding click event on delete button which is inside ...

  1. event in backbone view
  2. backbone event click outside view
  3. trigger event in backbone view

this is my code currently: let array = [`1 (1).jgp`,`1 (2).jgp`,`1 (3).jgp`,`1 (4).jgp`,`1 (5).jgp`,`1 (6).jgp`,`1 (7).jgp`,`1 (8).jgp`,`1 (9).jgp`,`1 (10).jgp`,`1 (11).jgp`] //rest of the code Is ...

this is my code currently: let array = [`1 (1).jgp`,`1 (2).jgp`,`1 (3).jgp`,`1 (4).jgp`,`1 (5).jgp`,`1 (6).jgp`,`1 (7).jgp`,`1 (8).jgp`,`1 (9).jgp`,`1 (10).jgp`,`1 (11).jgp`] //rest of the code Is ...

How to set value for input="color" when typing in input(.color-scheme__log-input-text), for example if I type #060606 - this color have to be applied to input="color" как изменить цвет input="color" ...

How to set value for input="color" when typing in input(.color-scheme__log-input-text), for example if I type #060606 - this color have to be applied to input="color" как изменить цвет input="color" ...

  1. value input color
  2. input value color css
  3. input value color change
  4. input value color html
  5. set value input color
  6. value input type color
  7. change input value color css
  8. input value text color
  9. input value font color
  10. change input value color jquery
  11. bootstrap input value color
  12. input value background color
  13. jquery input value color
  14. change input value color using jquery
  15. input value default color
  16. input color value rgb
  17. javascript change input value color
  18. get value of input color jquery
  19. get value of input color
  20. input color value not working

I am pretty new in Angular 2. I am studying how to create modules into an Angular app and I have the following doubt related a tutorial that I am following. My doubt is related to the routing. So in ...

I am pretty new in Angular 2. I am studying how to create modules into an Angular app and I have the following doubt related a tutorial that I am following. My doubt is related to the routing. So in ...

  1. what exact meaning of love
  2. what is the exact meaning of crush
  3. what is the exact meaning of ego
  4. what is the exact meaning of attitude
  5. what is the exact meaning of dating
  6. what is the exact meaning of crush in hindi
  7. what is the exact meaning of life
  8. what is the exact meaning of lol
  9. what is the exact meaning of virgin
  10. what is the exact meaning of peer tutoring
  11. what is the exact meaning of civilization
  12. what is the exact meaning of bae
  13. what is the exact meaning of democracy
  14. what is the exact meaning of memorial day
  15. what is the exact meaning of merry christmas
  16. what is the exact meaning of science
  17. what is the exact meaning of crush in tamil
  18. what is the exact meaning of global warming
  19. what is exact meaning of serverless computing in cloud
  20. what is the exact meaning of corruption