String Replace prototype method in javaScript

The String Replace String prototype method in javaScript comes in handy when it comes to most text search and replace tasks involving regular expressions.

1 - String Replace method basics

In order to use the the String.replace string prototype object it is important to have a fairly solid grasp on regular expressions. I will be covering some examples of them here in this post of course, but I will not be getting into regular expressions in detail here, as I have a post where I have done so when it comes to regex.

The basic idea of String.replace is to come up with a regular expression that will match the one or more patterns that I want to replace with something else, and use that pattern as the first argument for String.replace. The second argument then is the string to replace all matches for the pattern in the string to which the prototype method is being called off of.

1
2
3
4
5
let str = 'It is a bad day to do bad things for bad people',
result = str.replace(/bad/g, 'good');
console.log(result);
// 'It is a good day to do good things for good people'

2 - Using a function to create replacement strings

In place of a static string as the replacement, a function can be used to generate replacement strings that will differ depending on the nature of the instance of the pattern match. For example if the patter contains numbers or dates they can be extracted and used to generate the result in the resulting string.

1
2
3
4
5
6
let str = 'Some numbers for you are 2, 6, and 10 also.',
result = str.replace(/\d+/g, (num) => Math.pow(2, num));
console.log(result);
// 'Some numbers for you are 4, 64, and 1024 also.'