Search code examples
regexrubyjekyllliquid

How do I use a regular expression to select liquid tags within quotation marks, but not necessarily following/followed by quotation marks


I'd like to select all occurrences of the following liquid tags:

{%-

-%}

when they occur within quotation marks but not necessarily immediately following or followed by quotation marks.

Attempted several, think I am getting closer with: (?<=")({%-)(?=")

Very new to regex - I appreciate your help.

Example of what I am attempting to do:

{%- assign foo = true -%}

<span class="{%- if foo == true -%} blue{%- else -%} red{%- endif -%}">item</span>

then use regex to replace {%- and -%} only if within/next to ":

{%- assign foo = true -%}   (no replacement)

<span class="{% if foo == true %} blue{% else %} red{% endif %}">item</span>  (replaced)

Thank you very much for your expertise and assistance.


Solution

  • I'm guessing that maybe you might be trying to write an expression similar to:

    (?=.*")-%|(?=.*")%-
    

    and replace it with %.

    Demo

    (?=.*") will check for at least a " in the line, which you can change that.