简体   繁体   中英

Regex matching contiguous curly brackets

I am trying to match some curly bracket in HTML for custom built client side templating using regex. I have got it working except in the one case there are 2 or more matching strings next to each other.

The string I am trying to match is:

{{+ANYTEXT}}

the regex being used is:

{{\s*\+\S*\s*}}

So the following example:

<div class="rows"> 
    {{#Rows}} ssss1212XXXX {{+Anything.More.Evenmore}}$ssds {{/Rows}} 
</div> 
<div class="actions"> 
    {{#Actions}} ss[{{+UI.Action1}} {{+UI.Action2}} sdfsf {{/Actions}} 
    {{#Actions}} sss{{+UI.Action3}}{{+UI.Action4}} sdfsf {{/Actions}} 
</div>

Matches the first instance: {{+Anything.More.Evenmore}}; The next 2 actions as individual matches, Action1 and Action2 BUT matches the last 2 actions as one match.

So I have trying to figure how to match {{+ANYTEXT}} with anything except } and { at either end but not include them in the match. Unfortunately I have failed. Any help much appreciated.

Add a question mark to the \\S* to make it less greedy and it works. Test

/{{\s*\+\S*?\s*}}/g

You can use the regex :

({{\s*\+[^}]*\s*}})

DEMO

Explanation :

在此输入图像描述

Fiddle Demo

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM