English
German

Javascript IndexOf

JavaScript String indxof

IndexOf Ever want to find something in one of your JavaScript strings? Maybe even start your search halfway through the string? Well you're in luck! The string function indexOf lets you supply one required argument (the search query) and one optional argument (the offset) for all your simple searching needs.
If you are looking for a more powerful search feature, you should check out JavaScript's string search function, which has support for regular expressions, but not offsets.


String indxof Function

As we mentioned above, indexof has two arguments, with the second one being optional:

   1. SearchString - What you would like to search for.
   2. Offset (optional) - How far into the string you would like the search to begin. If you want to search the whole string, omit this argument.
 


indxof Function Example

To start off with, we will be finding the position of the "www" in a typical URL string. The indexof function will return the location of the first match that it encounters.

Example:

<script type="text/javascript">
var aURL = "http://www.hiscript.com/";
var aPosition = aURL.indexOf("www");

document.write("The position of www  =  " + aPosition);
</script>
 

Output
The position of www = 7

If you'd like to follow along with how this function does it's counting, here's a complete breakdown:

    * h - 0 - no match
    * t - 1 - no match
    * t - 2 - no match
    * p - 3 - no match
    * : - 4 - no match
    * / - 5 - no match
    * / - 6 - no match
    * w - 7 - maybe match
    * w - 8 - maybe match
    * w - 9 - It's a match, return the position of the start of the match (7).

Let's see what happens when we add another www to the URL string to see if this function gets confused or not.


indxof Function Offset Example

If we use the indexof function to find the first "www", then we can use that position + 1 as a way to skip the first "www" and find the second.

Example:

<script type="text/javascript">
var aURL = "http://www.hiscript.com/www.html";
var aPosition = aURL.indexOf("www");
var secondPos = aURL.indexOf("www", aPosition + 1);

document.write("The position of www  =  " + aPosition);
document.write("<br />The position of the second www  =  " + secondPos);

</script>
 

Output
The position of www = 7 The position of the second www = 21

By using an offset of 8 (7 + 1), we were able to skip the first "www" to instead find the second. In case you're interested in what the function was actually looking at after we gave an offset, we have it below:

    * ww.hiscript.com/www.html

There are only 2 w's left at the beginning of the string, due to the offset, making it impossible for a match to be found at the first set of "www".


Javascript Tutorial,Javascript IndexOf, Javascript IndexOf example, learn Javascript IndexOf,explain example Javascript IndexOf online free training Javascript Tutorial, Javascript Tutorial example, learn Javascript IndexOf, online tutorial, download tutorial, Javascript Tutorial books, Javascript Tutorial videos, live videos Javascript Tutorial, learn Javascript Tutorial, Javascript Tutorial topic Javascript IndexOf, live training Javascript Tutorial, download free tutorial