PandaMastr
PandaMastr

Reputation: 695

Exclude curly braces between and get the string - regex

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

Answers (2)

Rahul Kumar
Rahul Kumar

Reputation: 3157

Using regex and string.match()

const regex = /{+(.*?)}+/i;
const fruit = "{{Banana}}";
console.log(fruit.match(regex)[1]);

Upvotes: 1

DecPK
DecPK

Reputation: 25398

You can use /[{}]/g to get only the Banana

enter image description here

var regexp = /[{}]/g;
var fruit = "{{Banana}}";
console.log(fruit.replace(regexp, ""));

or /{{(.*?)}}/ with match

enter image description here

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

Related Questions