replace()
String
instance method
JS Home <<
JS Reference <<
String <<
replace()
Description
Returns a new string containing some or all of the matches of a replaced pattern.
- The pattern to replace can be a string or a regular expresion.
- For each match the pattern can be replaced by another string or the result of a called function.
Syntax
Signature | Description |
---|---|
aString.replace(regexp|substr, replacementSubstr|function) | Returns a new string containing some or all of the matches of a replaced pattern. |
Parameters
Parameter | Description |
---|---|
regexp | A regular expression or RegExp object or substr . |
substr | A part of the string to be replaced. |
replacementSubstr | The string to replace substr or one of the following patterns:
|
function | An invoked Function with the following arguments:
|
Examples
The code below replaces part of a string.
// Create a string variable.
var aNewSentence = 'I like to shine my nose';
// Replace part of the string.
alert(aNewSentence.replace('nose', 'shoes'));
Related Tutorials
JavaScript Basic Tutorials - Lesson 8 - Strings