'How do I escape quotation marks in liquid?
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 the 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)?
Solution 1:[1]
You don't need to escape it. The following will return true:
{% assign name = 'James "Jim" Smith' %}
{% if name contains '"' %}
true
{% else %}
false
{% endif %}
Solution 2:[2]
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.
Solution 3:[3]
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"
Solution 4:[4]
With Liquid map I have observed \\\" translates into \"
Solution 5:[5]
If none of the above works for you, try this. It did it for me when removing double quotes:
| remove '"'
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | GeorgeButter |
| Solution 2 | Adrian Mole |
| Solution 3 | Wesely |
| Solution 4 | Amit Chaudhary |
| Solution 5 | Dawid Dahl |
