var sounds = { 'word1': new Audio('sound1.mp3'), 'word2': new Audio('sound2.mp3'), }; var text = 'word1 word2 word3'; var word = 'word1'; if (text.search(word) > -1) sounds[word].play();