JavaScript RegEx to match custom formatted code block -


the code looks this:

{% item in example %}    {{ item }} {% endfor %} 

basically, need 3 things in code. item, example, , content of code block. i'm using expression:

^{% (.*?) in (.*?) %}((.*?|\n)+){% endfor %}$ 

but if have more 1 code in format, matches first opening of for until last closing of endfor. screenshot might explaining this:

regex

i have here: https://regex101.com/r/zd1vo9/3

add ? after + non-greedy match.

^{% (.*?) in (.*?) %}((.*?|\n)+?){% endfor %}$ 

demo

or

^{% (.*?) in (.*?) %}([\s\s]+?){% endfor %}$ 

demo


Comments