How to match words in a string that are wrapped in curly brackets using RegEx with TypeScript

1 Answer

0 votes
const str: string = "This is a {string} with {multiple} {words} wrapped in {curly} brackets.";

// Define the RegEx pattern
const regex: RegExp = /\{([^}]+)\}/g;

// Find all matches
const matches: any[] = [];
let match: any;
while ((match = regex.exec(str)) !== null) {
  matches.push(match[1]); // Capture group 1: text inside the brackets
}

console.log("Matches:", matches);

  
  
/*
  
 "Matches:",  ["string", "multiple", "words", "curly"] 
      
*/

 



answered Mar 18 by avibootz
...