![](/img/trans.png)
[英]Match a string between two other strings with regex in javascript
[英]Regex expression to match string between two other strings
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>
我正在尝试从字符串{%for ...%}和{%endfor%}的任何循环中获取内容
我已经尝试过,但是无法
/({%for(!?%})+%})((!? endfor)*){%endfor%} / gm
但是没用
我认为您应该使用与此类似的模式
正则表达式
(?<={% for[^%]*%})((?:.|\n)*)(?={% endfor %})
说明
(?<={% for[^%]*%})
:使用后向搜索模式{% for[^%]*%}
(?={% endfor %})
:使用超前搜索文本{% endfor %}
((?:.|\\n)*)
:for循环之间的一条消息,由变量$1
捕获
但是如果您的语言不支持环视,则只需使用
正则表达式
({% for[^%]*%})((?:.|\n)*)({% endfor %})
说明
({% for[^%]*%})
:捕获模式{% for[^%]*%}
到变量$1
({% endfor %})
:捕获模式{% endfor %}
到变量$3
((?:.|\\n)*)
:for循环之间的一条消息,由$2
捕获
只需根据您所用语言的限制修改我的regex
,即可完成此操作。
编辑
在搜索时,我认为Javascript
不支持环视,并且{
}
需通过\\
进行转义。 我已经使用一些在线正则表达式测试仪对Java进行了正则表达式测试,我明白了。
(\{% 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循环结束”
注意,我只是重新排列了我以前的正则表达式来完成这项稍微复杂的工作。
试试这个正则表达式:
{% 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 ... %}
和{% endfor %}
则可能会起作用:
/%}([\W\w]*){%/gm
说明 :
%} matches the characters %} literally
\W match any non-word character [^a-zA-Z0-9_]
\w match any word character [a-zA-Z0-9_]
{% matches the characters {% literally
g modifier: global. All matches (don't return on first match)
m modifier: multi-line.
范例 :
https://regex101.com/r/pM3oX7/1
尚不清楚您是否要在%} {%
内获取文本,还是要包含该部分。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.