Liquid Code Remove Everything Before First Pipe Instance In a Array with Two Pipes

Viewed 52

99% of our Shopify product titles look like this:

Brand Name | Product Name

I have added liquid code to remove the Brand Name and "|" pipe character:

{% assign product_title_trim = product.title |  split: "| " %}
{{product_title_trim[1]}}

This successfully removes the Brand Name and pipe leaving just "Product Name".

The issue is we have some products whose titles look like this:

Brand Name | Product Name | Variant Large

The goal is to 1. Remove the Brand Name, 2. Remove the First and Second Pipe "|", and 3. Keep the "Variant Large". The final title would look like this:

Product Name Variant Large

I know I need to 1. First split the array at the desired location ('Brand Name | '), 2. After the Array is split and the title is Product Name | Variant, remove the first pipe instance. Removing everything before the first, original pipe instance is proving to be more of a challenge. It is simple to do with a title with only one pipe. How can I remove everything before the first pipe instance, but leave the second pipe instance intact? Then, I can easily remove the second pipe instance. Any help would be greatly appreciated!

1 Answers

You can use the split and slice filters from the liquid code to get the desired result. You can also read more about these filters in the documentation:

split

slice

Use like this:

{{ product.title | split: '|' | slice: 1, 2 }}
Related