Function Updated 17 Oct 2026
String.replace(find, replace)
Returns a new string where the FIRST occurrence of find is replaced. To replace every occurrence, use replaceAll() or a regular expression with the g flag: text.replace(/a/g, "b").
In template mode there is also an older global helper replace(text, find, replacement) — three arguments, not a method — which replaces ALL occurrences.
Parameters
findstring | RegExprequiredText or regular expression to search for. Text is matched exactly (case-sensitive).
replacestring | functionrequiredReplacement text, or a function called for each match with (match, groups…, position, whole string) whose return value is inserted. With a text pattern, the replacement is inserted as is ($& and $1 are not interpreted).
Returns
Replaced text in string. Does not change the initial string.
Example
Mixed HTML (or any text file) with inline JavaScript between hash signs.
#{
var text = "hello world";
/* Replaces "hello" with "thank you" */
text = text.replace("hello", "thank you");
write(text);
}#The template above produces this output:
thank you world