The Java Script String object provides several methods that allow us to search for a character or substring within a string. For example, the code below finds the word "easy" in the sentence "code is easy to read".
Welcome to Bucaro TecHelp!

Welcome to Bucaro TecHelp!
Maintain Your Computer and Use it More Effectively
to Design a Web Site and Make Money on the Web

[About BTH]  [User Agreement]  [Privacy Policy]  [Site Map]  [Contact Form]  [Advertise on BTH]  [News Feed]

Google
Web
This Site

Get The Hottest Natural Health Newsletters Free!

Natural Health Newsletters Free!

[ ] Alternative Medicine
[ ] Anti-Aging
[ ] Aromatherapy
[ ] Back pain Relief
[ ] Herbal Remedies
[ ] Magnetic Therapy
[ ] Natural Nutrition
[ ] Vitamin Research
[ ] Weight Loss

Click here to see all of our newsletters, and sign up for the ones you want!

Join American Consumer Opinion™ and Get Paid Cash!
Get paid $4 to $25 for each survey
Typically $4 to $25 for each survey.
Click Here to join (Available worldwide)

Find a Character or a Substring Within a String

The Java Script String object provides several methods that allow us to search for a character or substring within a string. For example, the code below finds the word "easy" in the sentence "code is easy to read".

var strSentence = "code is easy to read";
var i = strSentence.indexOf("easy");

if(i < 0) alert("easy not found");
else alert("easy found at index: " + i);

After execution of the code, the variable i would contain the value 8. When the indexOf method cannot find the indicated substring in the string, it returns the value -1. As you see in the code above, we check for i being less than zero and then produce the proper message.

What if the sentence contains the word "easy" twice and we want to check only for the second one? In that case, we would provide the indexOf method with a starting index from which to begin the search.

var strSentence = "this easy code is easy to read";
var i = strSentence.indexOf("easy", 10);

if(i < 0) alert("easy not found");
else alert("easy found at index: " + i);

The code above starts searching for the substring at index 10, which is past the first substring "easy".

The string object also provides the lastIndexOf method that will search for a substring starting from the end of the string.

var strSentence = "this easy code is easy to read";
var i = strSentence.lastIndexOf("easy");

if(i < 0) alert("easy not found");
else alert("easy found at index: " + i);

The code shown above will return the same index as the previous example, without requiring a starting index. Even though it searches for a substring starting from the end of the string, it still returns the index relative to 0 being at the beginning of the string.

What if you want to check an email address to make sure that it does not have a dot at the end? That would indicate an invalid email address. You could use the String object's length property to determine the length of the email address string. Then use the lastIndexOf method with the length as a starting index to retrieve the last character of the email address.

var strEmail = "name@domain.com";
var len = strEmail.length;
var i = strEmail.lastIndexOf(".", len);

if(i == len - 1) alert("Invalid Email Adress");
else alert("Valid Email Adress");

Note in the code above that we subtract one from the length of the string before we compare it to the index of the dot. Remember, because the indexes start with 0, the length of the string will be one more than the highest index in the string.

Try the code above with and without the characters "com" on the end of the email address.

Web Design Sections
Java Script Quick Reference
Java Script Data Types
Java Script Reserved Words
Java Script Arithmetic Operators
Comparison Operators
Java Script Arrays
Java Script Character Encoding and Decoding
The if/else Structure
The switch/case Structure
The for Loop
The while Loop
The break Statement
The continue Statement
JavaScript Math Object
Round a Number
Determine Absolute Value
Determine Minimum and Maximum
Generating Random Numbers
Java Script Trigonometric Methods
Java Script Number Object
Format a Number as Currency
Java Script Strings
Compare Two Strings
Find a Character or a Substring Within a String
Include a Quote Character in a String
Include a Backslash Character in a String
Define Lines in a String
Use Escape to Replace Dangerous Characters
Convert a Number to a String
Convert a String to a Number
The Document Object Model (DOM)
Accessing Web Page Elements
Interactively Set Webpage Colors
Get Webpage File Date and File Size
Dueling Windows
Cookie Power Made Easy

[Site User Agreement]  [Advertise on This site]  [Search This Site]  [Contact Form]
Copyright©2001-2007 Bucaro TecHelp P.O.Box 18952 Fountain Hills, AZ 85269