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

using regex for filter's argument in liquid for designer #202

Closed
jojosati opened this issue Jun 7, 2013 · 4 comments
Closed

using regex for filter's argument in liquid for designer #202

jojosati opened this issue Jun 7, 2013 · 4 comments

Comments

@jojosati
Copy link

jojosati commented Jun 7, 2013

Could liquid parse some filter's argument as reqex instead of normal string ?

normal string
{{ 'foofoo' | replace:'foo','bar' }} #=> 'barbar'

regex pattern
{{'foofo' | replace:/foo.*/,'bar'}} #=> 'bar'

sathit

@nickpearson
Copy link
Contributor

I'm not sure how likely it is that this will be added to Liquid due to the security issues around regular expressions (example), but if you're working on a project where you can add custom filters, here's a simplified version of one that I use that accomplishes what you want:

def regexreplace(input, regex, replacement = '')
  input.to_s.gsub(Regexp.new(regex), replacement.to_s)
end

This is the same as the replace filter in StandardFilters, except that it converts the second parameter into a Regexp before passing it to String#gsub.

@jojosati
Copy link
Author

jojosati commented Jun 7, 2013

Thank you, but how to put it into shopify's liquid engine.

example

# prevent expose secret info in comment tag within product description
{{ product.description | replace: /\<\!--.*?--\>/, '' }}

@nickpearson
Copy link
Contributor

See the Create your own filters section of Liquid for Programmers on the wiki.

@fw42
Copy link
Contributor

fw42 commented Jun 14, 2013

Yes, a custom filter seems like a better solution here.

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

No branches or pull requests

3 participants