Javascript regex for Twig templates contents -


i use rich text editor allow online modification of twig templates. twig template engine using following syntax display variables:

{{ object.property }} 

it uses functions generate urls, such as:

{{ url('mr_message_read', {id: message.id}) }} 

now want display, next editor, list of twig variables and functions used in template. this, retrieve current content html twig "keywords" shown above. extract keywords, use regex below:

var reg = /{{[^}]+}}/g; var match = text.match(reg); console.log( match ); 

this work work example 1 not example 2, twig function requires } string. so, tried several other syntaxes allow "anything except }}". none of them seem fit:

    var reg = /{{[^}]+}}/g; // ignores second example     var reg = /{{[^}}]+}}/g; // idem     var reg = /{{[^}}]*}}/g; // idem     var reg = /{{(^}}+)}}/; // null     var reg = /{{(^}})+}}/; // null     var reg = /\{\{[^\}\}]+\}\}/g; // ignores second example     var reg = /\{\{[^}}]+\}\}/g; // ignores second example     var reg = /\{\{[^\}\}]+\}\}/g; // ignores second example     var reg = /\{\{[^[}}]]+\}\}/g; // ignores second example 

i'm struggling now. guess it's escaping issue, i'm stuck :)

sample content :

<p>{{ author.fullname }} wrote message. read here: <a href="{{ url('mr_message_read', {id: message.id}) }}">messagerie</a>.</p> <hr /> <blockquote> <p>{{ message.content|nl2br }}</p> </blockquote> 

edit: solution based on thomas code

function gettwigtags(){      var str = ckeditor.instances['form_content'].getdata();     var regex = /{{\s*([^{]*{([^{]*):\s*(.*?)}.*?|[^{]*)\s*}}/g;     var keywords = new array();     let m;      while ((m = regex.exec(str)) !== null) {         // necessary avoid infinite loops zero-width matches         if (m.index === regex.lastindex) {             regex.lastindex++;         }          // result can accessed through `m`-variable.         m.foreach((match, groupindex) => {             if(match !== 'undefined' && groupindex == 1)                 // console.log(`found match: group ${groupindex}: ${match}`);                 keywords[keywords.length] = match;         });     }      return keywords; } 

you can use code:

const regex = /{{\s*([^{]*{([^{]*):\s*(.*?)}.*?|[^{]*)\s*}}/g;  const str = `{{ url('mr_message_read', {id: message.id}) }}  {{ object.property }}`;  let m;    while ((m = regex.exec(str)) !== null) {      // necessary avoid infinite loops zero-width matches      if (m.index === regex.lastindex) {          regex.lastindex++;      }            // result can accessed through `m`-variable.      m.foreach((match, groupindex) => {        if(match !== 'undefined' && groupindex > 0)          console.log(`found match: group ${groupindex}: ${match}`);      });  }


Comments

Popular posts from this blog

php - How to display all orders for a single product showing the most recent first? Woocommerce -

asp.net - How to correctly use QUERY_STRING in ISAPI rewrite? -

angularjs - How restrict admin panel using in backend laravel and admin panel on angular? -