replace

replace

> Functions

Replaces all occurrences of search with replace in the string string.

Parameters
Return
TypeScript

In JavaScript, s.replace(search, by) only replaces the first occurrence when search is a string, whereas replace in LeekScript replaces them all: write s.replaceAll(search, by). Second trap: the replacement text is interpreted — $& stands for the matched text, $1 for a captured group: "ab".replaceAll("a", "$&$&") is "aab". Double the dollar sign to insert a literal one: "$$" gives "$".

Python

In Python, s.replace(search, by) replaces all occurrences, like replace in LeekScript, and a third argument limits how many: "aaa".replace("a", "b", 2) is "bba". The pattern is always a literal string, never a regular expression; for that, use the re module: re.sub("a+", "X", "aaab") is "Xb".

Examples

replace("Banana", "n", "t") // "Batata" replace("a,b,c,d,e,f", ",", "+") // "a+b+c+d+e+f" replace("Hello to the world", "Hello", "Welcome") // "Welcome to the world"