The replace method

The replace method is a method that can be used to replace a portion of string to another.

The replace method replaces the first match in a given string, leaving the remaining ones untouched. unlike it’s counter part replaceAll.

The function can optionally take a regular expression as the first parameter, this being a regular expression, you can specify the g flag and voila, it now works like a replaceAll

The replace method, can also take in a function as the second parameter, which simply will be called for each match.

The replace method, using regular expression, can make it such a joke to work with strings. you can group matches and use advanced patters to achieve what would otherwise take 40 more lines using the procedural way.

Search Method

Sometimes you would like to find a particular pattern in a string, while find is great for the job, it lacks one thing. It can not work with regular expression. Fortunately, the search method is there for the rescue, just like find, it returns the index of the found element, or -1 if the pattern is not present in a string.

But unlike find, it can work with regular expression seamlessly to allow you to craft super power matchers.