Pithikos
Pithikos

Reputation: 20310

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.

Upvotes: 11

Views: 15169

Answers (2)

Dave
Dave

Reputation: 483

I know this is ancient, but for anyone else coming across it: https://shopify.github.io/liquid/basics/operators/

contains contains checks for the presence of a substring inside a string.

{% if product.title contains "Pack" %} This product's title contains the word Pack. {% endif %}

contains can also check for the presence of a string in an array of strings.

{% if product.tags contains "Hello" %} This product has been tagged with "Hello". {% endif %}

contains can only search strings. You cannot use it to check for an object in an array of objects.

Upvotes: 4

David Jacquel
David Jacquel

Reputation: 52829

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]

Upvotes: 19

Related Questions