ÄúµÄλÖãºÑ°ÃÎÍøÊ×Ò³£¾±à³ÌÀÖÔ°£¾JavaScript£¾Javascript Tutorial
Javascript Tutorial

Javascript - Intro
Javascript - Syntax
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

Javascript String Search

Knowing if something is or isn't in a string can be very important. If you had an online forum and didn't want people to be able to create usernames that included swear words, you could use the search function to try to find bad words in username's and reject them if any were found.

String Search Function

This string function takes a regular expression and then examines that string to see if there are any matches for that expression. If there is a match then it will return the position in the string where the match was found. If there isn't a match then it will return -1. We won't be going into great depth about regular expressions, but we will show you how to search for words in a string.

Search Function Regular Expression

The most important thing to remember when creating a regular expression is that it must be surrounded with slashes /regular expression/. With that knowledge let's search a string to see if a common name "Alex" is inside it.

Javscript Code:

<script type="text/javascript">
var myRegExp = /Alex/;
var string1 = "Today John went to the store and talked with Alex.";
var matchPos1 = string1.search(myRegExp);

if(matchPos1 != -1)
    document.write("There was a match at position " + matchPos1);
else
    document.write("There was no match in the first string");


</script>

Display:

Notice that our regular expression was just the name "Alex". The search function then used this name to see if "Alex" existed in string1. A match was found and the position of the match (45) was returned.

String Search Function: Alternative Searches

Another basic tool for regular expressions is the pipe character "|" which allows you to search for alternative words /RegExp1|RegExp2/. Instead of just searching for just one word, we can now use the pipe character to search for multiple words.

Javscript Code:

<script type="text/javascript">
var myRegExp = /Alex|John/;
var string1 = "Today John went to the store and talked with Alex.";
var matchPos1 = string1.search(myRegExp);

if(matchPos1 != -1)
    document.write("There was a match at position " + matchPos1);
else
    document.write("There was no match in the first string");


</script>

Display:

Notice that our regular expression had two names: Alex and John. The search function then used these names to try to find the first occurrence in the string string1. John came before Alex in our string so its position (6) was returned.

Let's look at a couple more advanced examples.

Advanced Search Function Examples

The following examples play around with the names a little so you can clearly see how the search function operates.

Javscript Code:

<script type="text/javascript">
var myRegExp1 = /Tom|Jan|Alex/;
var string1 = "John went to the store and talked with Alexandra today.";
var matchPos1 = string1.search(myRegExp1);

if(matchPos1 != -1)
    document.write("The first string found a match at " + matchPos1);
else
    document.write("No match was found in the first string");

var myRegExp2 = >/Tom|Jan|Alex /;
var string2 = "John went to the store and talked with Alexandra today.";
var matchPos2 = string2.search(myRegExp2);
if(matchPos2 != -1)
    document.write("<br />The second string found a match at " + matchPos2);
else
    document.write("<br />No match was found in the second string");

var myRegExp3 = /Tom|Jan|Alexandra/;
var string3 = "John went to the store and talked with Alexandra today.";
var matchPos3 = string3.search(myRegExp3);
if(matchPos3 != -1)
    document.write("<br />The third string found a match at " + matchPos3);
else
    document.write("<br />No match was found in the third string");

var myRegExp4 = /Tom|Jan|Alexandra/;
var string4 = "John went to the store and talked with Alex today.";
var matchPos4 = string4.search(myRegExp4);
if(matchPos4 != -1)
    document.write("<br />The fourth string found a match at " + matchPos4);
else
    document.write("<br />No match was found in the fourth string");
</script>

Display:

In the first search a match was found. This is because our search didn't specify that the name had to be exactly Alex and because the name Alexandra contains "Alex" a match was found.

In the second search we fixed this by adding a space after the name Alex to now be "Alex " and no match was found.

In the third search we changed our expression to include "Alexandra" and found a match.

In the fourth and final search we changed our string to include "Alex" and we didn't find a match.