Javascript Reference
Categories: String

javascript String replace( )

@July 8, 2008, 4:03 p.m.
replace( ) Firefox/Netscape/NN 4 IE 4 ECMA 3  

replace(regexpression, replaceString)

  

Returns the new string that results when matches of the regexpression parameter are replaced by the replaceString parameter. The original string is unharmed in the process, so you need to capture the returned value in a variable to preserve the changes.

 
Parameters
 
  • regular expression object . If you want the replace( ) method to act globally on the string, set the global switch (g) on the regular expression. See the regular expression object for the syntax to create a regular expression object.
  • A string that is to take the place of all matches of regexpression in the current string.
 
Returned Value

A string.

Example

var str = 'abCdc';

str = str.replace(/c/ig,'1');

// abCdc -> ab1d1


Powered by Linode.