Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is regex in liquid possible?

On my Shopify page I have a bunch of products that have weird characters in their description that I assume got there from importing from a CSV. It would be too much work to go through each one and remove these characters manually.

I have this regex script to get rid of them on the client-side:

$(function() {
    var regex = /\?ÕÌ_|_Œ‚|[ŠŽÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÑÒÓÔÕÖØÙÚÛÜÝÞßðÿ_]/gi;

    $('.rte').html(function(i, oldHTML) {
        return oldHTML.replace(regex, ' ')
            .replace(/[^\x00-\x7F]|\?/g, '');
    });
});

But I would like to move something like this server side and possibly recreate it with liquid. Is this even possible to do with liquid? How can I use this regex server-side?

Edit:

I wrote this hacky code

{{ product.description | replace: '?Íí_' | replace: 'Œæ' | replace: 'äó»íˆí_í‚_' | replace: 'í_Œ‚í__' | replace: '̴Ì_' | replace: '?ÕÌ_' | replace: '?í´íë_' | replace: 'Ì_åÇÌ__' | replace: 'åÊ' | replace: '?’«íëí__' | replace: '?í_ŒÇí_í‚_' | replace: 'í«í_' | replace: '?í?í__' | replace: '’Çíë_' | replace: 'íë_í«íˆíë__' | replace: 'íëí__’Çíëíæíëí___' | replace: '’Ç', '' }}

But this only works if I know what the specific strings are I need to replace instead of searching from a group of strings like in my javascript regex.

like image 425
ByteSettlement Avatar asked Dec 29 '16 18:12

ByteSettlement


2 Answers

You can use jekyll-regex-replace Ruby Gem, or write your own plugin if you need to tweak it further. I'm not 100% sure about your use-case if this matches it 100%, but something along this line:

  1. Add to your _config.yml:
plugins:
  ...
  - jekyll-regex-replace
  1. In your Liquid template:
{{ product.description | regex_replace: '\?ÕÌ_|_Œ‚|[ŠŽÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÑÒÓÔÕÖØÙÚÛÜÝÞßðÿ_]', '' }}

If you are with a host which limits Jekyll plugins (https://pages.github.com/versions/) then I can only advise to move to another free provider which does not, for example GitLab, that's what I did as well.

  1. In case of GitLab you also need to add into your Gemfile:
group :jekyll_plugins do
  ...
  gem "jekyll-regex-replace", "~> 1.1.0"

Notes for GitHub: you maybe able to define your own custom Jekyll build with a GitHub Action. That's also a possibility instead of moving.

like image 147
Csaba Toth Avatar answered Oct 16 '22 11:10

Csaba Toth


If you have weird characters in your product descriptions, that stem from your CSV, why don't you just clean up your CSV, and then re-import clean description text? Save yourself the grief. Removing weird characters is an indication you messed up the encoding of your data. UTF-8 is your friend here.

Using a Liquid replace filter will work for you, but that is uber-hackey... best to find the source of the error, and fix that. Removing those characters is going to be risky as in some cases you might be fixing the display of some words, but in others mangling will still likely be there.

Note that the Liquid replace filter is all you get to work with server-side, it is your regex option, even though it is far from the usual regex capability.

like image 1
David Lazar Avatar answered Oct 16 '22 10:10

David Lazar