正则表达式,用于匹配Liquid模板中的标签:在html标记内 [英] Regex for matching a tag in a Liquid template : ">" inside html tag
问题描述
我必须为 Liquid 模板中的 body 标记编写匹配模式.
尽管匹配HTML标签非常简单,但我有一个问题,可以在Liquid代码中使用HTML特殊字符.示例:
< body class ="template-{{template | replace:'.',''| truncatewords:1,''}} {%如果促销%}有促销{%endif%} {%如果products.size>1个%}具有相关产品{%endif%} {%如果settings.product-hover =='quick-shop'%}有快速购物{%endif%}正在加载>
或简化:
< body {%bla>1%} bla bla>
我当前的匹配模式/< body(.[^>] *)>/s
匹配上述代码,直到第一个>
.我需要一个与整个标签匹配的模式.
尝试使用:
/< body(.[^> {}] *(?:{+ [^}] *} + [^> {}] *)*)>/s
代替 [^>]*
正则表达式使用 [^>{}]*(?:{+[^}]*}+[^>{}] *)*
,匹配除>
, {
或}
之外的任何字符;在某个时候它可以满足 {
,因此它匹配 {+ something} +
的全部内容,然后再匹配 [^> {}] *
.最后一个 *
重复了很多次.
I have to write a match pattern for the body tag in a Liquid template.
While matching HTML tags is pretty straightforward, I have the problem that HTML special character can be used inside the Liquid code. Example:
<body class="template-{{ template | replace: '.', ' ' | truncatewords:
1, '' }}{% if promo %}has-promo{% endif %} {% if products.size > 1
%}has-related-products{% endif %} {% if settings.product-hover ==
'quick-shop' %}has-quick-shop{% endif %} loading" >
or simplified:
<body {% bla > 1 %} bla bla>
My current match pattern /<body(.[^>]*)>/s
matches the above code until the first >
. I need a pattern that matches the whole tag.
Try with:
/<body(.[^>{}]*(?:{+[^}]*}+[^>{}]*)*)>/s
Instead of [^>]*
the regex uses [^>{}]*(?:{+[^}]*}+[^>{}]*)*
, that matches any character but >
, {
or }
; at some point it can meet a {
, so it matches the whole content of {+something}+
, followed again by [^>{}]*
. This trick is repeated many times with the last *
.
这篇关于正则表达式,用于匹配Liquid模板中的标签:在html标记内的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!