Search code examples
functionsubstringjekyllliquid

Get substring from string in Liquid?


I am working with Jekyll and I have the string balh blah blah&garbage **&*&% garbage <h1>TITLE</h1> &^*$%"

Is there a way to grab TITLE? I have looked at the functions here but I don't see something that I can use.


Solution

  • split to the rescue !

    {% assign str = 'garbage <h1>TITLE</h1> moregarbage' %}
    {% assign a = str | split: '<h1>' %}
    

    We now have garbage in a[0] and TITLE</h1> moregarbage in a[1]

    {% assign b = a[1] | split: '</h1>' %}
    

    We now have TITLE in b[0] and moregarbage in b[1]