How do I create a regex to exactly match {{ variable } and ignore the {{ variable }}.
this what I have
text.match(/{{[^}]+}/g
but it's also matching {{ variable }} instead of just {{ variable }.
How do I create a regex to exactly match {{ variable } and ignore the {{ variable }}.
this what I have
text.match(/{{[^}]+}/g
but it's also matching {{ variable }} instead of just {{ variable }.
If you want to detect {{ variable } as valid and ignore {{ variable }}
You have to verify the end of the string using $
Regex Needed /{{[^}]+}$/g
Visulization
const input1 = "{{ variable }";
console.log(input1.match(/{{[^}]+}$/g));
const input2 = "{{ variable }}";
console.log(input2.match(/{{[^}]+}$/g));
EDIT
If you want to detect { variable }} as valid and ignore {{ variable }}
You have to verify the start of the string with ^, also you have to check for negative lookahead for {{ using (?!{{)
Regex Needed /(?!{{)^{[^}]+}}$/g
Visulization
const input1 = "{ variable }}";
console.log(input1.match(/(?!{{)^{[^}]+}}$/g));
const input2 = "{{ variable }}";
console.log(input2.match(/(?!{{)^{[^}]+}}$/g));