Reputation: 695
To be honest i am not very good at regex . But basically what i need to do is to extract / render the string between the curly braces .
What i have tried until now is :
var regexp = /\{\{(.*?)\}\}/
var fruit = '{{Banana}}'
console.log(fruit.replace(regexp , ''));
//Output : ""
The idea is to have only the string Banana
displayed without the curly braces.
I will be glad if someone can land me a hand . Thanks upfront
Upvotes: 0
Views: 168
Reputation: 3157
Using regex
and string.match()
const regex = /{+(.*?)}+/i;
const fruit = "{{Banana}}";
console.log(fruit.match(regex)[1]);
Upvotes: 1
Reputation: 25398
You can use /[{}]/g
to get only the Banana
var regexp = /[{}]/g;
var fruit = "{{Banana}}";
console.log(fruit.replace(regexp, ""));
or /{{(.*?)}}/
with match
var regexp = /{{(.*?)}}/;
var fruit = "{{Banana}}";
const result = fruit.match(regexp)[1];
console.log(result);
or if it supports positive look ahead and positive lookbehind
var regexp = /(?<={{).*(?=}})/;
var fruit = "{{Banana}}";
const result = fruit.match(regexp)[0];
console.log(result);
Upvotes: 1