-2

Hi I am only starting out with Regexs, and am trying to write a regular expression to find string inside curly brackets while keeping curly brackets

Text: "This is a {nice} text to search for a {cool} substring".
Answer I want: {nice} and {cool}

I have already looked through the answer here: Regular expression to find string inside curly brackets Javascript

This almost does what I want, except that it only returns what's inside the curly brackets. I want to keep the curl brackets.

1 Answers1

1
var myString = "This is a {nice} text to search for a {cool} substring",
    pattern = /\{[^{}]*\}/g;

console.log(myString.match(pattern));

That's a modification of the answer to the question you linked. I added the { to the beginning of the pattern, and I removed the lookahead around the } at the end.