javascript - js regular expression match between two sets of delimiters, that might occur many times -


am trying retrieve data exists between 2 different delimiters,

where pattern might occur multiple times regular expression far

/\@\((\w*)\)\{\{\{([^]*)\}\}\};/

this works single match, , occationaly match entire set correctly if there other data between sets of delimiters, not case in situations, match

for example

@(name){{{ contains bunch of arbitrary text save later when @name occurs again, replaced this. }}}; @name @(broken){{{   broken match }}}; 

will normaly capture pattern

contains bunch of arbitrary text save later when @name occurs again, replaced this. }}}; @name @(broken){{{   broken match 

i know need non greedy evaluation, not sure where.

i have intent of eventualy needing take pattern farther, , nest, resulting in structures such following:

@(name){{{   @(anothername){{{     contains bunch of arbitrary text save later     when @name occurs again, replaced this.   }}}   @anothername; }}}; @name @(broken){{{   broken match }}}; 

though cannot use regular expressions alone, , recursion beyond scope of question, proven wrong though.

edit:

i had tried given patterns previously, no avail, accepted answer , example provided proved me not regular expression problem, rather issue program it's self.

i looping replacements, because possible 1 replacement might add new instance of string replaced, , require additional expansion. setting test value true, make loop start on again, , never setting false.

always small.

you can make non-greedy adding ? after * , use modifier g make global :

/\@\((\w*)\)\{\{\{([^]*?)\}\}\};/g 

see demo.

as can see matches following part separately :

@(name){{{   @(anothername){{{     contains bunch of arbitrary text save later     when @name occurs again, replaced this.   }}}   @anothername; }}}; 

and

@(broken){{{   broken match }}}; 

and doesn't match nested patters.its because of of regex engines doesn't support nested matching because in case you'll fall context free grammers , opposed of regular languages


so proper way such tasks can use appropriate parser.


Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -