I have the string bellow:
JavaScript
x
$string = '
@if(topLevel)
{{
<form method="post">
@if(bottomLevel)
{{
<input>
}}
</form>
}}';
What i’m trying is to find the value inside the @if(.*?) and the content between the brackets of the top level “@if”.
This is the pattern i’ve tried:
JavaScript
@if((.*?))(s|n|t)+{{(s|n|t)(.*?)(s|n|t)}}
This is matching:
JavaScript
@if(bottomLevel)
{{
<input>
}}
How can i go from the outside into the string and match the top level “@if”?
Advertisement
Answer
Use singleline flag, and remove lazy from .*?
:
JavaScript
@if((.*?))s+{{s*(.*)s*}}
And since TAB is included in s
, and with the singleline flag n
as well, you only need s
to match the white space.
Edit
Here’s an alternative using recursion, to manage multiple groups of braces.
JavaScript
@if((.*?))s+{{((?:[^{}]|(?R))*)}}