Logo

Programming-Idioms

History of Idiom 63 > diff from v33 to v34

Edit summary for version 34 by programming-idioms.org:
New JS implementation by user [programming-idioms.org]

Version 33

2016-11-30, 21:24:22

Version 34

2018-08-09, 13:08:04

Idiom #63 Replace fragment of a string

Assign to x2 the value of string x with all occurrences of y replaced by z.
Assume occurrences of y are not overlapping.

Idiom #63 Replace fragment of a string

Assign to x2 the value of string x with all occurrences of y replaced by z.
Assume occurrences of y are not overlapping.

Extra Keywords
substring substitute
Extra Keywords
substring substitute
Code
var x2 = x.replace(new RegExp(y, 'g'), z);
Comments bubble
This works well only if y doesn't contain special regexp characters.
Doc URL
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace
Origin
https://stackoverflow.com/a/17606289/871134