Friday, May 17, 2024
 Popular · Latest · Hot · Upcoming
70
rated 0 times [  71] [ 1]  / answers: 1 / hits: 41224  / 12 Years ago, tue, march 20, 2012, 12:00:00

i am new to regex. I am trying to parse all contents inside curly brackets in a string. I looked up this post as a reference and did exactly as one of the answers suggest, however the result is unexpected.



Here is what i did



var abc = test/abcd{string1}test{string2}test //any string
var regex = /{(.+?)}/
regex.exec(abc) // i got [{string1}, string1]
//where i am expecting [string1, string2]


i think i am missing something, what am i doing wrong?



update



i was able to get it with /g for a global search



var regex = /{(.*?)}/g
abc.match(regex) //gives [{string1}, {string2}]


how can i get the string w/o brackets?


More From » regex

 Answers
16
test/abcd{string1}test{string2}test.match(/[^{}]+(?=})/g)


produces



[string1, string2]


It assumes that every } has a corresponding { before it and {...} sections do not nest. It will also not capture the content of empty {} sections.


[#86718] Monday, March 19, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
jazminuniquer

Total Points: 63
Total Questions: 121
Total Answers: 96

Location: Cambodia
Member since Thu, May 21, 2020
4 Years ago
;