1

I have a string in this format

"{{abc}}, {{def}}, some text {{ghi}}, some other text {{jkl}}"

And I would like to replace each of {{...}} with some string based on what is ... (that comes out of json and not an issue) and stuck with what regex pattern to use. Closest I could come was to /{{(.*)}}/gi, but that returns the whole string as the same has {{ and }} at ends. Here's the code I have tried

let str = "{{abc}}, {{def}}, some text {{ghi}}, some other text {{jkl}}";
let regex = /{{(.*)}}/gi;
let result;
let indices = [];
let results = [];
while ((result = regex.exec(str))) {
    indices.push(result.index);
    results.push(result[0]);
}

Any help will be highly appreciated

2
  • Try escaping the {/}. i.e. \{\{(.*)\}\} Commented Oct 6, 2020 at 6:12
  • 1
    Also try .*?, i.e. \{\{(.*?)\}\} Commented Oct 6, 2020 at 6:16

1 Answer 1

2

This will do it: \{\{(.*?)\}\}

You need to escape the } via \, also set the non greedy operator ?

See: https://regex101.com/r/F9xdgx/2

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.