String.replace() (Method)

Searches a string using a regular expression and replace the matches.


ECMAScript edition - 3
JavaScript - 1.2
JScript - 3.0
Internet Explorer - 4.0
Netscape - 4.0
Property/method value type:String primitive
JavaScript syntax:-myObject.replace(aPattern, aString)
Argument list:aPatternA regular expression pattern
aStringA replacement string

This is one of the additions to the String object to support regular expressions.

The search pattern locates matches, which are then replaced by the string value in the second argument.

The value is modified in place.

The replacement text can make use of the numbered sub-expression references and can use them in the replaced string.


Example code:

   // Simple replacement
   myString = "javascript, 'JAVASCRIPT', JavaScript";
   myString.replace(/javascript/ig, "JavaScript");
   myString.replace(/^.*\'([^\']*)\'.*$/, "---''$1''---")

See also:RegExp pattern, RegExp.exec(), RegExp.lastIndex, Regular expression, String.match(),, String.split()


ECMA 262 edition 3 - section -