Bredele
Bredele

Reputation: 115

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

Upvotes: 4

Views: 5103

Answers (1)

Sarah
Sarah

Reputation: 1331

/{([^{}]+)}/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}"

Upvotes: 7

Related Questions