How do I escape quotation marks in liquid?

Viewed 7488

I am working with an input string like 'James "Jim" Smith' with a person's nickname being double quoted within a string. I have been trying to find if a person has a nickname by using the contains operator, but it never finds entries with double quotes. As an example:

<-- Name - James "Jim" Smith -->
{% if Name contains '\"' %}
   Do Something
{% else %}
   Always gets here
{% endif %}

How can I search for the literal double quote in a string using contains (or split)?

6 Answers

I found solution for this problem. This is the example for my approach.

{{ post.title | remove: '& quot;'}}

Note: There should be no space between & and quot – I added this to show exactly.

With Liquid map I have observed \\\" translates into \"

I have read about escaping both single and double quotes by doubling them and had some success myself doing this...

In my own example I was trying to remove quotes from a string And the following did NOT work:

{{ post.title | remove: """ }}

However this DID work:

{{ post.title | remove: """" }}

I think that would work in your if statement:

{% if name contains '""' %}

But am not sure how that would work where you are assigning the string or if you need to escape it in your "assign"

If none of the above works for you, try this. It did it for me when removing double quotes:

| remove '"'

Remove: '"' seems working to me

Related