Javascript 正则表达式来匹配其他两个字符串之间的字符串

Javascript 正则表达式来匹配其他两个字符串之间的字符串,javascript,regex,Javascript,Regex,javascript中的另一个硬正则表达式: 我有这个字符串: <td style="padding:0 2%">{% for product in products.pos01 %} <table class="box-item" item-editable="" style="float:left;padding-bottom:10px; width:32%;border-spacing: 0px; border: 0px; border-collapse:

javascript中的另一个硬正则表达式:

我有这个字符串:

    <td style="padding:0 2%">{% for product in products.pos01 %}    
<table class="box-item" item-editable="" style="float:left;padding-bottom:10px; width:32%;border-spacing: 0px; border: 0px; border-collapse: collapse;"><tr><td style=" padding:10px 5px; vertical-align:top ">
<a href="**|product.url|**" class="button view-deal" style="padding:8px 10px; background-color: #fc5d26; text-decoration:none; display:inline-block; text-align:center; color:#fff; font-size:12px;margin:0px; line-height:140%;text-transform:uppercase">view deal</a></div></td></tr></table>{% if (loop.index0-2) is divisibleby 3 %}</td></tr><tr>
<td style="padding:0 2%">{% endif %}{% endfor %}
 </td>
{products.pos01%}
{%if(loop.index0-2)可被3%整除}
{%endif%}{%endfor%}
我试图从字符串{%for…%}和{%endfor%}获取任何循环中的内容

我试过了,但没办法

/({%for(!?%})+%})((!?endfor)*{%endfor%}/gm

但是不起作用

试试这个正则表达式:

{%for[^\0]+?{%endfor%}

说明:

{% for            # search for text `{% for `
[^\0]+?           # while not input's end
{% endfor %}      # search for the next `{% endfor %}`

或者,如果您想要分组:

{% (for [^\0]+?)%}([^\0]+?){% endfor %}

希望有帮助。

试试这个正则表达式:

{%for[^\0]+?{%endfor%}

说明:

{% for            # search for text `{% for `
[^\0]+?           # while not input's end
{% endfor %}      # search for the next `{% endfor %}`

或者,如果您想要分组:

{% (for [^\0]+?)%}([^\0]+?){% endfor %}

希望有帮助。

试试这个正则表达式:

{%for[^\0]+?{%endfor%}

说明:

{% for            # search for text `{% for `
[^\0]+?           # while not input's end
{% endfor %}      # search for the next `{% endfor %}`

或者,如果您想要分组:

{% (for [^\0]+?)%}([^\0]+?){% endfor %}

希望有帮助。

试试这个正则表达式:

{%for[^\0]+?{%endfor%}

说明:

{% for            # search for text `{% for `
[^\0]+?           # while not input's end
{% endfor %}      # search for the next `{% endfor %}`

或者,如果您想要分组:

{% (for [^\0]+?)%}([^\0]+?){% endfor %}


希望有帮助。

我想您应该使用类似的模式

Regex

(?<={% for[^%]*%})((?:.|\n)*)(?={% endfor %})
({% for[^%]*%})((?:.|\n)*)({% endfor %})
解释

({%表示[^%]*%})
:捕获模式
{%表示[^%]*%}
到变量
$1

({%endfor%})
:捕获模式
{%endfor%}
到变量
$3

((?:.|\n)*)
:由
$2捕获的for循环之间的消息

只要根据您语言中的限制修改我的
regex
,您就可以这样做了

编辑

当我搜索时,我认为
Javascript
不支持查找,另外一件事
{
}
需要通过
\
进行转义。我已经使用一些在线的Javascript正则表达式测试仪测试了正则表达式,我得到了这个结果

(\{% for[^%]*%\})((?:.|\n)*)(\{% endfor %\})
要获得所需的文本,只需使用变量
$2

附加的

如果您想在嵌套循环内捕获消息,例如

示例消息

{% for product in products.pos01 %} 
    ...
    {% for product in products.pos02 %}
         "messages"
    {% endfor %}
    ...
{% endfor %}
要在这个嵌套循环中捕获
“messages”
,只需将我以前的正则表达式修改为

(\{% for[^%]*%\}(?:.|\n)*\{% for[^%]*%\})((?:.|\n)*)(\{% endfor %\}(?:.|\n)*\{% endfor %\})
解释

(\{%for[^%]*%\}(?:.|\n)*\{%for[^%]*%\})
:表示“for循环的开始”,后跟“包括换行符在内的任何字符”,后跟“for循环的开始”

(((?:.|\n)*)
:我们的目标消息

(\{%endfor%\}(?:.\n)*\{%endfor%\})
:表示“for循环结束”,后跟“包括换行符在内的任何字符”,后跟“for循环结束”


请注意,我刚刚重新安排了我以前的正则表达式,以完成这项稍微复杂一点的工作。

我想您应该使用类似于此的模式

Regex

(?<={% for[^%]*%})((?:.|\n)*)(?={% endfor %})
({% for[^%]*%})((?:.|\n)*)({% endfor %})
解释

({%表示[^%]*%})
:捕获模式
{%表示[^%]*%}
到变量
$1

({%endfor%})
:捕获模式
{%endfor%}
到变量
$3

((?:.|\n)*)
:由
$2捕获的for循环之间的消息

只要根据您语言中的限制修改我的
regex
,您就可以这样做了

编辑

当我搜索时,我认为
Javascript
不支持查找,另外一件事
{
}
需要通过
\
进行转义。我已经使用一些在线的Javascript正则表达式测试仪测试了正则表达式,我得到了这个结果

(\{% for[^%]*%\})((?:.|\n)*)(\{% endfor %\})
要获得所需的文本,只需使用变量
$2

附加的

如果您想在嵌套循环内捕获消息,例如

示例消息

{% for product in products.pos01 %} 
    ...
    {% for product in products.pos02 %}
         "messages"
    {% endfor %}
    ...
{% endfor %}
要在这个嵌套循环中捕获
“messages”
,只需将我以前的正则表达式修改为

(\{% for[^%]*%\}(?:.|\n)*\{% for[^%]*%\})((?:.|\n)*)(\{% endfor %\}(?:.|\n)*\{% endfor %\})
解释

(\{%for[^%]*%\}(?:.|\n)*\{%for[^%]*%\})
:表示“for循环的开始”,后跟“包括换行符在内的任何字符”,后跟“for循环的开始”

(((?:.|\n)*)
:我们的目标消息

(\{%endfor%\}(?:.\n)*\{%endfor%\})
:表示“for循环结束”,后跟“包括换行符在内的任何字符”,后跟“for循环结束”


请注意,我刚刚重新安排了我以前的正则表达式,以完成这项稍微复杂一点的工作。

我想您应该使用类似于此的模式

Regex

(?<={% for[^%]*%})((?:.|\n)*)(?={% endfor %})
({% for[^%]*%})((?:.|\n)*)({% endfor %})
解释

({%表示[^%]*%})
:捕获模式
{%表示[^%]*%}
到变量
$1

({%endfor%})
:捕获模式
{%endfor%}
到变量
$3

((?:.|\n)*)
:由
$2捕获的for循环之间的消息

只要根据您语言中的限制修改我的
regex
,您就可以这样做了

编辑

当我搜索时,我认为
Javascript
不支持查找,另外一件事
{
}
需要通过
\
进行转义。我已经使用一些在线的Javascript正则表达式测试仪测试了正则表达式,我得到了这个结果

(\{% for[^%]*%\})((?:.|\n)*)(\{% endfor %\})
要获得所需的文本,只需使用变量
$2

附加的

如果您想在嵌套循环内捕获消息,例如

示例消息

{% for product in products.pos01 %} 
    ...
    {% for product in products.pos02 %}
         "messages"
    {% endfor %}
    ...
{% endfor %}
要在这个嵌套循环中捕获
“messages”
,只需将我以前的正则表达式修改为

(\{% for[^%]*%\}(?:.|\n)*\{% for[^%]*%\})((?:.|\n)*)(\{% endfor %\}(?:.|\n)*\{% endfor %\})
解释

(\{%for[^%]*%\}(?:.|\n)*\{%for[^%]*%\})
:表示“for循环的开始”,后跟“包括换行符在内的任何字符”,后跟“for循环的开始”

(((?:.|\n)*)
:我们的目标消息

(\{%endfor%\}(?:.\124;\ n)*\{%endfor%\})
:表示“l的结束