Javascript Match Search Method


match Method

Description

Returns, as an array, the results of a search on a string using a supplied Regular Expression object.

Syntax

stringObj.match(rgExp)

The match method syntax has these parts:

Part Description
stringObj Required. The String object or literal on which to perform the search.
rgExp Required. The regular expression to use in the search.
Remarks

The match method, which behaves like the exec method, returns an array of values. Element zero of the array contains the last matched characters. Elements 1...n contain matches to any parenthesized substrings in the regular expression.

The method updates the contents of the RegExp object.

The following example illustrates the use of the match method:

function MatchDemo()
{
  var r, re;
  var s = "The quick brown fox jumped over the lazy yellow dog.";
  re = /fox/i;
  r = s.match(re);
  return(r);
}

exec Method

Description

Executes a search for a match in a specified string.

Syntax

rgexp.exec(str)

The exec method syntax has these parts:

Part Description
rgexp Required. A Regular Expression object. Can be a variable name or a literal.
str Required. The string to perform a search on.
Remarks

The results of an exec method search are placed into an array.

If the exec method does not find a match, it returns null. If it finds one or more matches, the exec method returns an array, and the RegExp object is updated to reflect the results of the search.

The following example illustrates the use of the exec method:

function ExecDemo()
{
  var s = "AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPp"
  var r = new RegExp("g", "i");
  var a = r.exec(s);
  document.write(a);
  r.compile("g");
  var a = r.exec(s);
  document.write(a);
}

RegExp Object

Description

Stores information on regular expression pattern searches.

Syntax

RegExp.propertyname

The propertyname argument is one of the RegExp object properties.

Remarks

The RegExp object cannot be created directly, but is always available for use. Its properties have undefined as their value until a successful regular expression search has been completed.

The following example illustrates the use of the RegExp object:

function matchDemo()
{
  var s;
  var re = new RegExp("d(b+)(d)","ig");
  var str = "cdbBdbsbdbdz";
  var arr = re.exec(str);
  s = "$1 contains: " + RegExp.$1 + "<BR>";
  s += "$2 contains: " + RegExp.$2 + "<BR>";
  s += "$3 contains: " + RegExp.$3;
  return(s);
}

replace Method

Description

Returns a copy of a string with text replaced using a regular expression.

Syntax

stringObj.replace(rgExp, replaceText)

The replace method syntax has these parts:

Part Description
stringObj Required. The String object or literal on which to perform the replace. This object is not modified by the replace method.
rgExp Required. A Regular Expression object describing what to search for.
replaceText Required. A String object or literal containing the text to replace for every successful match of rgExp in stringObj.
Remarks

The result of the replace method is a copy of stringObj after all replacements have been made.

The method updates the contents of the RegExp object.

The following example illustrates the use of the replace method:

function ReplaceDemo()
{
  var r, re;
  var s = "The quick brown fox jumped over the lazy yellow dog.";
  re = /fox/i;
  r = s.replace(re, "pig");
  return(r);
}

In addition, the replace method can also replace subexpressions in the pattern. The following example swaps each pair of words in the string:

function ReplaceDemo()
{
  var r, re;
  var s = "The quick brown fox jumped over the lazy yellow dog.";
  re = /(\S+)(\s+)(\S+)/g;
  r = s.replace(re, "$3$2$1");	// Swap each pair of words.
  return(r);
}

search Method

Description

Returns the position of the first substring match in a regular expression search.

Syntax

stringObj.search(rgexp)

The search method syntax has these parts:

Part Description
stringObj Required. The String object or literal to search.
rgexp Required. A Regular Expression object containing the pattern to search for.
Remarks

The search method indicates if a match is present or not. If a match is found, the search method returns an integer value that indicates the offset from the beginning of the string where the match occurred. If no match is found, it returns -1. To get further information, use the match method.

The following example illustrates the use of the search method:

function SearchDemo()
{
  var r, re;
  var s = "The quick brown fox jumped over the lazy yellow dog.";
  re = /fox/i;
  r = s.search(re);
  return(r);
}

test Method

Description

Returns a Boolean value that indicates whether or not a pattern exists in a searched string.

Syntax

rgexp.test(str)

The test method syntax has these parts:

Part Description
rgexp Required. A Regular Expression object. Can be a variable name or a literal.
str Required. The string to test a search on.
Remarks

The test method checks to see if a pattern exists within a string and returns true if so, and false otherwise.

The RegExp object is not modified by the test method.

The following example illustrates the use of the test method:

function TestDemo(re, s)
{
  var s1;
  // Test string for existence of regular expression.
  if (re.test(s))
    s1 = " contains ";
  else
    s1 = " does not contain ";
  // Get text of the regular expression itself.
  return(s + s1 + re.source);
}