Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding replace function #1855

Merged
merged 7 commits into from
Feb 17, 2014
Merged

Conversation

jakebellacera
Copy link
Contributor

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.

Use case: high-DPI image mixin

.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.

jakebellacera and others added 7 commits August 23, 2013 15:45
* renamed gsub function to replace
* reordered replace's function arguments to be much more readable
* added support for replacement (Regexp) options
@lukeapage
Copy link
Member

I'm ok with this. If no-one complains I will pull into the next release.

@jakebellacera
Copy link
Contributor Author

Great!

@jonschlinkert
Copy link
Contributor

Love it! I think it's great

lukeapage added a commit that referenced this pull request Feb 17, 2014
@lukeapage lukeapage merged commit 6117a33 into less:master Feb 17, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants