- CoffeeScript - Home
- CoffeeScript - Overview
- CoffeeScript - Environment
- CoffeeScript - command-line utility
- CoffeeScript - Syntax
- CoffeeScript - Data Types
- CoffeeScript - Variables
- CoffeeScript - Operators and Aliases
- CoffeeScript - Conditionals
- CoffeeScript - Loops
- CoffeeScript - Comprehensions
- CoffeeScript - Functions
- CoffeeScript Object Oriented
- CoffeeScript - Strings
- CoffeeScript - Arrays
- CoffeeScript - Objects
- CoffeeScript - Ranges
- CoffeeScript - Splat
- CoffeeScript - Date
- CoffeeScript - Math
- CoffeeScript - Exception Handling
- CoffeeScript - Regular Expressions
- CoffeeScript - Classes and Inheritance
- CoffeeScript Advanced
- CoffeeScript - Ajax
- CoffeeScript - jQuery
- CoffeeScript - MongoDB
- CoffeeScript - SQLite
CoffeeScript String - replace() method
Description
This method finds a match between a regular expression and a string, and replaces the matched substring with a new substring.
The replacement string can include the following special replacement patterns −
| Pattern | Inserts |
|---|---|
| $$ | Inserts a "$". |
| $& | Inserts the matched substring. |
| $` | Inserts the portion of the string that precedes the matched substring. |
| $' | Inserts the portion of the string that follows the matched substring. |
| $n or $nn | Where n or nn are decimal digits, inserts the nth parenthesized submatch string, provided the first argument was a RegExp object. |
Syntax
Given below is the syntax of replace() method of JavaScript. We can use the same method from the CoffeeScript code.
string.replace(regexp/substr, newSubStr/function[, flags]);
This method accepts various arguments as follows −
regexp - A RegExp object. The match is replaced by the return value of parameter #2.
substr - A String that is to be replaced by newSubStr.
newSubStr - The String that replaces the substring received from parameter #1.
function - A function to be invoked to create the new substring.
flags - A String containing any combination of the RegExp flags: g - global match, i - ignore case, m - match over multiple lines. This parameter is only used if the first parameter is a string.
Example
The following example demonstrates the usage of replace() method of JavaScript in CoffeScript code. Save this code in a file with name string_replace.coffee
re = /apples/gi
str = "Apples are round, and apples are juicy."
newstr = str.replace re, "oranges"
console.log newstr
Open the command prompt and compile the .coffee file as shown below.
c:\> coffee -c string_replace.coffee
On compiling, it gives you the following JavaScript.
// Generated by CoffeeScript 1.10.0
(function() {
var newstr, re, str;
re = /apples/gi;
str = "Apples are round, and apples are juicy.";
newstr = str.replace(re, "oranges");
console.log(newstr);
}).call(this);
Now, open the command prompt again and run the CoffeeScript file as shown below.
c:\> coffee string_replace.coffee
On executing, the CoffeeScript file produces the following output.
oranges are round, and oranges are juicy.