Home | Javascript validator | JavaScript Editor JavaScript EditorGet Advanced
JavaScript and Ajax Editor,
Validator and Debugger!

1st JavaScript Editor.
Microsoft® JScript®
replace Method
 Language Reference 
Version 3 

See Also                  Applies To


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);
}

Home | Javascript validator | JavaScript Editor JavaScript EditorGet Advanced
JavaScript and Ajax Editor,
Validator and Debugger!

1st JavaScript Editor.