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.

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 , 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.

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.

JavaScript 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:

There was a match at position 45
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 "|" (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.

JavaScript 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:

There was a match at position 6
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.

JavaScript 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:

The first string found a match at 39
No match was found in the second string
The third string found a match at 39
No match was found in the fourth string
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.