JavaScript Searching for a Word in a String

Today I’m going to write about an exercise that shows up on the JavaScript track at Codecademy. The exercise in question is there to facilitate the practice of certain concepts and not really to create a way of searching for a word. As a matter of fact the instructor suggests to think of other ways to implement such search and this is the reason why I’ve decided to answer the question by showing one possible way of doing it. I will be using indexOf().

indexOf() is a JavaScript method and I go into more detail about it on my eBOOK.

Let’s start with the code itself:

var text = "Bla bla Tony bla la Tony bla bla Tony bla bla bla Tony";
var searchWord = "Tony";
var init = text.indexOf(searchWord);
var hits = [];

for (var i = init; i < searchWord.length + init; i++) {

The idea is to search for the word assigned to the variable seachWord. Then use a for loop to push all the word’s characters into an array named hits.

init is a variable that gets assigned to the result of applying the indexOf() method to the variable seachWord.

indexOF() is very useful here because it returns the position of the first character of the word searched in reference to its location in the variable text. In contrast to the method used at Codecademy’s exercise, this character must be part of the whole word passed to the method indexOf(), not just any character that matches. This is because indexOf() matches the complete word and it is therefore more to the point of what we are trying to accomplish.

By using indexOf() we also limit the result to the first match as opposed to matching all similar words in text.

I hope this inspires you to create other possibilities.

You can read more about indexOf() on my eBook JavaScript Objects Functions and Arrays Explained


Author: tarau

technical writer, web technologies

4 thoughts on “JavaScript Searching for a Word in a String”

  1. I was noticing that the original code that you were expected to product on CodeAcademy listed your name everytime it came across it. Mine of course included a capitalized letter that wasn’t part of my name (that happened to be at the end) so it spat out something that wasn’t my name. It came out with 4 strings of 11 characters.

    Yours stops after finding the name once and logs that? How would you make this so it logs all the instances where your name appears?

  2. Hi Chabuku, thanks for your comment and sorry for the delay. indexOf is very useful to spot one instance of the name. However, if you want to display all instances and avoid all the other unrelated characters you really need to convert the text variable into an array by using split(” “).The ‘quotes, space, quotes’ in the parentheses, will separate the characters every time JS encounters a white space. The final product will be an array of separate words. Then we can use a for loop to push all instances of myName into array hits. Then it is just a matter of displaying hits to screen. You can find a sample of this script on my server:

  3. hey man, maybe im too late to the party but here is much more efficient code, that will return every occurrence of your name and count how many times it appears for you:

    var text = “tyran test test test tyran test test test tyran”;
    var res = text.match(/tyran/gi);

    console.log(“name appears ” + (res.length) + ” times”);

    you could easily modify this for custom user input using the prompt commands.

Leave a Reply

Please log in using one of these methods to post your comment: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s