Javascript Tutorial Example
Example
Javascript Array
JavaScript Boolean
JavaScript Date
JavaScript Math
JavaScript Number
JavaScript String
Javascript Tutorial
Javascript Introduction
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 Comparison
JavaScript Switch
JavaScript Try Catch
JavaScript Special Text
JavaScript Throw
JavaScript Math
JavaScript RegExp
Javascript String
Javascript Strings
Javascript Length
Javascript Split
Javascript Search
Javascript Replace
Javascript IndexOf
Javascript Compare
Javascript Advanced
Javascript getElementById
Javascript innerHTML
JavaScript Browser
JavaScript Cookies
JavaScript Animation
JavaScript Image Maps
JavaScript Timing
JavaScript Create Objects
JavaScript Summary
Knowing if something is or isn't in a string can be very important. If you have an online forum and don't want people to be able to create usernames that include swear words, you can use the search function to find bad words in usernames and reject them if any were found.
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 , it will return the position in the string where the match was found. If there isn't a match, 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.
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.
<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>
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.
Another basic tool for regular expressions is the pipe character "|" (it's below the Backspace key on standard keyboards) 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.
<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>
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.
The following examples play around with the names a little so you can clearly see how the search function operates.
<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>
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 make our search for "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.

