Tizag Tutorials
Web www.tizag.com
Home Web TutorialsFirst Website Guide HTML Tutorial CSS Tutorial
Templates Website Templates Flash TemplatesWebsite Logos
Scripting Javascript Tutorial PHP Tutorial PERL Tutorial Ajax Tutorial ASP Tutorial VBScript Tutorial
Databases SQL Tutorial MySQL Tutorial
Miscellaneous XML Tutorial Web Host Guide Flash Tutorial Javascript Tutorial Javascript - Intro Javascript - Syntax Javascript - Enable Javascript - Location Javascript - External Javascript - Operators Javascript - Variables Javascript - Functions Javascript - Events Javascript - Statements Javascript - If Javascript - Else If Javascript - While Javascript - For Loop Javascript - Comments Javascript - Array Javascript - Alert Javascript - Confirm Javascript - Prompt Javascript - Print Javascript - Redirect Javascript - Pop Up Javascript - Date Javascript - Form Javascript - Void 0 Javascript String Javascript - Strings Javascript - Length Javascript - Split Javascript - Search Javascript - Replace Javascript - indexOf Javascript - Compare Javascript Advanced Javascript - getElementById Javascript - innerHTML Contact Us
Javascript String indexOf
Ever want to find something in one of your Javascript strings? Maybe even want to 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 indexOf Function
As we mentioned above, indexOf has two arguments, 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 then omit this argument.
indexOf 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.
Javscript Code:
Display:
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.
Javscript Code:
Display:
The position of www = 7
Looks like it returned the position of the first "www", just as we expected, so no surprises in this function. However, what would we do if we wanted to find the second "www"? Well, we'd have to use an offset for starters.
indexOf Function Offset Example
If we use the indexOf function to find the first "www" then we could use that position + 1 as a way to skip the first "www" and find the second.
Javscript Code:
Display:
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" and 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.tizag.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".
* Go Back
* Continue
Web Reference
HTML Reference CSS Reference CSS Examples PHP Examples Sponsors
HostGator.com
SEO Book
Help Tizag Grow
Link to Tizag
New - Tizag.com Forums!
Recent Forum Topics:
- hello
- ASP (VBScript) Drop Down Menu from SQL DB
- problem with frame scrolling
- Minimum Free space for shrinking volume???
More Tutorials!
Microsoft Office Tutorials Artist Tutorials
2003-2008 Erack Network | Copyright | Privacy Policy | Advertising Information
Site design by Seattle Web Design
tes aja
Selasa, 13 November 2007
0
komentar
Diposting oleh
anumurama
di
05.09
0
komentar
Label: tes aja
Langganan:
Postingan (Atom)

2007