JavaScript indexOf Tutorial

Source: Internet
Author: User

Ever want to find one of your JavaScript strings? Even start searching for half the string? Then you're in your good luck! The string function IndexOf allows you to provide an argument (search query), as well as an optional parameter (offset) to all of your simple search requirements.


If you're looking for a more powerful search feature, you should look at JavaScript's string search feature, which supports regular expressions but not offsets.

String music indexof function
As we have mentioned above, IndexOf has two arguments, and one of the second options is:

SearchString-you're looking for.
Offset (optional)-How far the string you want to search begins. If you want to search the entire string, ignore this argument.
IndexOf function as an example
To begin, we will find a typical URL string for the position "www". The IndexOf function will return to the position of the first game of ability.

<script type= "Text/javascript" >
var aurl = "http://www.tizag.com/";
var aposition = Aurl. indexOf ("www");

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

Location is 7

If you want to follow up with this function how it's the ticket, here's the total crash:

h-0-no match
Pen-1-does not match
T-2-does not match
P-3-No Match
:-4-does not match
/-5-does not match
/-6-does not match
W-7-maybe Match
Watt-8-maybe Match
W-9-This is a race to return the position of the start of the race (7).
Let's see what happens when we add the WWW URL string to see if this function is confusing or not.

<script type= "Text/javascript"
var aurl = "Http://www.tizag.com/ var aposition = aurl.indexof ("www");

document.write ("The position of www  = " + aposition);

</script>

result.

The position of www = 7

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.