String.replace

Finds a match between a regular expression and a string, and replaces the matched substring with a new substring.

Method of String

Syntax

replace( regexp, newSubStr )
replace( regexp, function )

Parameters

Parameter Description
regexp The name of the regular expression. It can be a variable name or a literal.
newSubStr The string to put in place of the string found with regexp. This string can include the RegExp properties RegExp.$1, ..., $9, RegExp.lastMatch, RegExp.lastParen, RegExp.leftContext, and RegExp.rightContext.
function A function to be invoked after the match has been performed.

Description

This method does not change the String object it is called on; it simply returns a new string.
If you want to execute a global search and replace, or a case insensitive search, include the g (for global) and i (for ignore case) flags in the regular expression. These can be included separately or together. The following two examples below show how to use these flags with replace.

Specifying a function as a parameter

When you specify a function as the second parameter, the function is invoked after the match has been performed. (The use of a function in this manner is often called a lambda expression.)

In your function, you can dynamically generate the string that replaces the matched substring. The result of the function call is used as the replacement value.

The nested function can use the matched substrings to determine the new string (newSubStr) that replaces the found substring. You get the matched substrings through the parameters of your function. The first parameter of your function holds the complete matched substring. Other parameters can be used for parenthetical matches, remembered
submatch strings. For example, the following replace method returns XX.zzzz - XX , zzzz.

The array returned from the exec method of the RegExp object and the subsequent match is available to your function. You can use the content of the array plus the input and the index (index of match in the input string) properties of the array to perform additional tasks before the method replaces the substring.

Examples

Example 1. In the following example, the regular expression includes the global and ignore case flags which permits replace to replace each occurrence of 'apples' in the string with 'oranges.'

This example produces the result "oranges are round, and oranges are juicy."

Example 2. In the following example, the regular expression is defined in replace and includes the ignore case flag.

This example produces the result "Twas the night before Christmas..."
Example 3. The following script switches the words in the string. For the replacement text, the script uses the values of the $1 and $2 properties.

This example produces the result "Smith, John".
Example 4. The following example replaces a Fahrenheit degree with its equivalent Celsius degree. The Fahrenheit degree should be a number ending with F. The function returns the Celsius number ending with C. For example, if the input number is 212F, the function returns 100C. If the number is 0F, the function returns -17.77777777777778C.
The regular expression test checks for any number that ends with F. The number of Fahrenheit degree is accessible to your function through the parameter $1. The function sets the Celsius number based on the Fahrenheit degree passed in a string to the f2c function. f2c then returns the Celsius number. This function approximates Perl's s///e flag.

Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.