less/less.js — pull request #1855 by jakebellacera (2014-02-07 08:04:44+00:00)
Title: Adding replace function

This patch adds support for a `replace` function that replaces text using regex which was previously discussed in #1511. I believe that this function should be in the core library because string replacement is a basic feature that has a multitude of use cases.

#### `replace(subject, pattern, replacement, flags)`

This function returns a String.

* **subject** - (String) the string to search and replace.
* **pattern** - (String) the pattern to search for.
* **replacement** - (String) the string to replace the matched text.
* **flags** - (String, optional) string of [regular expression flags](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions#Advanced_Searching_With_Flags)

## Use case: high-DPI image mixin

```scss
.hd-background-image (@src, @size) {
background-image: url(@src);

@media only screen and (-webkit-min-device-pixel-ratio 2, min-resolution 2dppx) {
@hdSrc: replace(@src, "\.([a-zA-Z]{3,4})$", "_2x.$1");

background-image: url(@hdSrc);
background-size: @size;
}
}
```

The mixin above will load the normal background image for normal DPI devices and the background image with `_2x` appended to the filename for high DPI devices.