óCoffeeScript Cookbook

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

"JavaScript is my favorite!".replace /Java/, "Coffee"
# => 'CoffeeScript is my favorite!'

"foo bar baz".replace /ba./, "foo"
# => 'foo foo baz'

"foo bar baz".replace /ba./g, "foo"
# => 'foo foo foo'

The second version takes 2 arguments: pattern and callback function

"CoffeeScript is my favorite!".replace /(\w+)/g, (match) ->
  match.toUpperCase()
# => 'COFFEESCRIPT IS MY FAVORITE!'

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.