Text Extraction
Text Between Curly Braces Regex
Template engines, string formatting functions and translation files often mark variables with curly braces, for example Hello {name}. This pattern escapes the opening brace, takes everything that is not a brace, and requires a closing brace. Group 1 gives the placeholder name, which you can use to list every variable a template expects or to check that a translation keeps the same placeholders as the source text. It also works on double-brace syntax such as {{user}}, where it matches the inner pair and returns user. Escaping the braces keeps engines from reading them as a repetition quantifier.
Pattern (JavaScript)
/\{([^{}]*)\}/Matches the character "{", then a group (any character except {}, zero or more times), then the character "}".
Examples
Matches
- ✓ Hello {name}
- ✓ Total: {amount} PKR
- ✓ {{user}} logged in
Does not match
- ✗ no braces
- ✗ {open only
- ✗ close only}
Try it
4 matches (JavaScript engine)
Hello {name} Total: {amount} PKR {{user}} logged in no braces {open only close only}
Code in 8 languages
Converted automatically from the JavaScript pattern; language-specific notes are shown where the syntax or support differs.
JavaScript
const re = /\{([^{}]*)\}/g;
const text = "Hello {name}";
const matches = [...text.matchAll(re)].map((m) => m[0]);
console.log(matches);Python
import re
pattern = re.compile(r'\{([^{}]*)\}')
text = 'Hello {name}'
matches = [m.group(0) for m in pattern.finditer(text)]
print(matches)PHP
<?php
$pattern = '/\{([^{}]*)\}/';
$text = 'Hello {name}';
preg_match_all($pattern, $text, $matches);
print_r($matches[0]);Java
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
public static void main(String[] args) {
Pattern pattern = Pattern.compile("\\{([^{}]*)\\}");
String text = "Hello {name}";
Matcher matcher = pattern.matcher(text);
while (matcher.find()) {
System.out.println(matcher.group());
}
}
}Go
package main
import (
"fmt"
"regexp"
)
func main() {
re := regexp.MustCompile(`\{([^{}]*)\}`)
text := "Hello {name}"
fmt.Println(re.FindAllString(text, -1))
}C#
using System;
using System.Text.RegularExpressions;
var pattern = new Regex(@"\{([^{}]*)\}");
var text = "Hello {name}";
foreach (Match m in pattern.Matches(text))
{
Console.WriteLine(m.Value);
}Rust
// Cargo.toml: regex = "1"
use regex::Regex;
fn main() {
let re = Regex::new(r"\{([^{}]*)\}").unwrap();
let text = "Hello {name}";
for m in re.find_iter(text) {
println!("{}", m.as_str());
}
}- Add regex = "1" under [dependencies] in Cargo.toml.
Ruby
pattern = /\{([^{}]*)\}/
text = 'Hello {name}'
matches = text.to_enum(:scan, pattern).map { Regexp.last_match[0] }
p matchesFrequently asked questions
- Why escape curly braces when some engines accept them bare?
- Braces form quantifiers such as {2,5}, and with the u flag JavaScript rejects lone braces outright. Escaping them is portable across every flavor.
- How do I extract only double-brace placeholders like {{name}}?
- Use \{\{\s*([^{}]+?)\s*\}\} instead. It requires two braces on each side and trims spaces around the name.
- Can this pattern parse JSON objects?
- No. JSON objects nest and contain strings with braces in them, which a single regex cannot balance. Use JSON.parse for JSON and keep this pattern for flat placeholders.