Replacing Substrings
Problem
You need to replace a portion of a string with another value.
Solution
Use the JavaScript replace
method. replace
matches with the given string, and returns the edited string.
The first version takes 2 arguments: pattern and string replacement
The second version takes 2 arguments: pattern and callback function
The callback function is invoked for each match, and the match value is passed as the argument to the callback.
Discussion
Regular Expressions are a powerful way to match and replace strings.