To match only fooand barwithout surrounding braces, you can use
(?<=(?<!\{)\{)[^{}]*(?=\}(?!\}))
if your language supports lookbehind statements.
Explanation:
(?<=
(?<!\{)
\{
)
[^{}]*
(?=
\}
(?!\})
)
EDIT: In JavaScript, you have no lookbehind. In this case, you need to use something like this:
var myregexp = /(?:^|[^{])\{([^{}]*)(?=\}(?!\}))/g;
var match = myregexp.exec(subject);
while (match != null) {
for (var i = 0; i < match.length; i++) {
}
match = myregexp.exec(subject);
}
source
share