Unfortunately, despite having tried to learn regex at least one time a year for as many years as I can remember, I always forget as I use them so infrequently. This year my new year’s resolution is to not try and learn regex again – So this year to save me from tears I’ll give it to Stack Overflow. (Last Christmas remix).
I want to pass in a string in this format {getThis}, and be returned the string getThis. Could anyone be of assistance in helping to stick to my new year’s resolution?
Related questions on Stack Overflow:
If your string will always be of that format, a regex is overkill:
substring(1means to start one character in (just past the first{) and,g.length-1)means to take characters until (but not including) the character at the string length minus one. This works because the position is zero-based, i.e.g.length-1is the last position.For readers other than the original poster: If it has to be a regex, use
/{([^}]*)}/if you want to allow empty strings, or/{([^}]+)}/if you want to only match when there is at least one character between the curly braces. Breakdown:/: start the regex pattern{: a literal curly brace(: start capturing[: start defining a class of characters to capture^}: ‘anything other than}‘]: OK, that’s our whole class definition*: any number of characters matching that class we just defined): done capturing}: a literal curly brace must immediately follow what we captured/: end the regex pattern