Logo

Programming-Idioms

History of Idiom 63 > diff from v34 to v35

Edit summary for version 35 by "><marquee>:
[JS] "><marquee>

Version 34

2018-08-09, 13:08:04

Version 35

2019-09-26, 18:37:54

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