Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex and Handlebars

I'm currently working on a set of components (http://github.com/bredele) that you can assemble to get the same kind of features that you have in some JavaScript framework such as Olives.js, Canjs or Angular.

One of the component allows you to bind live dom to an object with handlebars.I use the regex (/{([^}]+)}/g) to match only simple handlebars. I would like to match double handlebars as following:

before:

{label} //return 'label'

after:

{label} //return 'label'

{{label}} //return '{label}'

The regex should allow the two use cases. Any idea?

Thanls

like image 239
Bredele Avatar asked Mar 21 '23 10:03

Bredele


1 Answers

/{([^{}]+)}/g should have the behavior you describe:

"cat dog".replace(/{([^{}]+)}/g, "$1") => "cat dog"
"{cat} {dog}".replace(/{([^{}]+)}/g, "$1") => "cat dog"
"{{cat}} {{dog}}".replace(/{([^{}]+)}/g, "$1") => "{cat} {dog}"
like image 197
Sarah Avatar answered Apr 01 '23 20:04

Sarah