Skip to content
Advertisement

How to get brackets value from a document / string in node.js

I am trying to get the value inside the brackets of a string. for example:

const mystring = "I am a {job} and my name is {name}. my age is {age} years old"

how am I be able to get the value inside the brackets (job, age and name)?

I actually use doctemplater npm to fill data to my document template. But since the template will vary, I need to know which value is required to be inserted the data.

Thanks

Advertisement

Answer

You can use regex

const mystring =
  "I am a {job} and my name is {name}. my age is {age} years old";

const regex = /[^{}]+(?=})/g;
console.log(mystring.match(regex));
Advertisement