1

I have these declared:

var obj = {aaa: "came", bbb: "saw", ccc: "conquered", zzz: "discarded" };

var str = "I {aaa}, I {bbb}, I {ccc}. Then I {aaa} again."

What I want to do is str.replace() each curly braced string with the appropriately named property from obj. So in the end, I should have:

I came, I saw, I conquered. Then I came again.

Thank you.

EDIT: The linked duplicate indeed proved very appropriate for my case. In addition, answers on that question actually provide dynamic solutions for the case where I did not know the properties of obj ahead of time.

2
  • Possible duplicate of How to replace string {} value to obj (key value) Commented Jun 15, 2019 at 8:26
  • 2
    Downvoter, in case it's due to the duplicate flag, please forgive ;) I actually searched SO a whole lot before going ahead to post a new question. Perhaps that answer didn't come up high on the list because the title has no "RegExp" and no "Javascript"! Commented Jun 15, 2019 at 8:52

3 Answers 3

4

Capture what comes between {}s, and use a callback for the replacer to look up that captured property on the object:

var obj = {aaa: "came", bbb: "saw", ccc: "conquered", zzz: "discarded" };
var str = "I {aaa}, I {bbb}, I {ccc}. Then I {aaa} again.";

const output = str.replace(/{([^}]+)}/g, (_, prop) => obj[prop]);
console.log(output);

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

5 Comments

Hey, could you explain how the regex captures everything between {}? How does [^}]+ capture all characters? What is specifically the point of ^} ? Thanks
@Andreas thanks for the link. Though I'm still confused how [^}] represents "Match a single character not present in the list below". Why is it not ?
@kemicofa It matches "one or more characters which are not }". So the whole pattern means "match {, repeatedly match anything but }, then match }". You could also use {(.*?)}, but that's less efficient
@kemicofa [...] match any character in the list, [^...] match anything that is not (^) in the list
@Andreas ahh ok. I was thinking of ^ as in first element. Thanks.
3

You can use callback function of replace method

var obj = {aaa: "came", bbb: "saw", ccc: "conquered", zzz: "discarded" };
var str = "I {aaa}, I {bbb}, I {ccc}. Then I {aaa} again."

let op = str.replace(/\{([^}]+)\}/g, (_,g1)=> obj[g1] || _)

console.log(op)

1 Comment

Great and elegant! Absolutely new syntax to me. Thank you.
2

Easiest and elegant approach is to use string interpolation

var obj = {aaa: "came", bbb: "saw", ccc: "conquered", zzz: "discarded" };

var str = `I ${obj.aaa}, I ${obj.bbb}, I ${obj.ccc}. Then I ${obj.aaa} again.`

console.log(str);

1 Comment

Thank you sir. However, this solution would require knowing each and every object property name ahead of time.

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.