-1

I'm working on a web app where users can create text templates. For example when the user is creating a new template they would write any variable text like so [[variable]].

Example:

Hi [[recipient]],

Here are your login credentials:

Username: [[username]]

Password: [[password]]

When the user submits the template form I need to search through the content string and get all the variables that they created and put them in an array. So for this example I'm trying to get an array that looks like this:

[recipient,username,password]
ulou
  • 5,542
  • 5
  • 37
  • 47
James Kehs
  • 11
  • 5
  • How good are your regular expression skills? – tadman Apr 19 '23 at 21:28
  • Not as good as they could be lol – James Kehs Apr 19 '23 at 21:29
  • 2
    Why not use something already implemented and not reinvent the wheel like using this https://mustache.github.io/ there are plenty of options there, you can simply tell users we are using "mustache" and save yourself a headache of documentation – Al-Mothafar Apr 19 '23 at 21:32
  • 1
    If you must roll your own, indexOf('[[',ix) and indexOf(']]', ix) and substring or substr. Remember that users will make mistakes, so [[ might not have a matching ]] – Dave S Apr 19 '23 at 21:33
  • The regexp is `/\[\[(.*?)\]\]/` – Barmar Apr 19 '23 at 21:41
  • Does this answer your question? [Format a JavaScript string using placeholders and an object of substitutions?](https://stackoverflow.com/questions/7975005/format-a-javascript-string-using-placeholders-and-an-object-of-substitutions) – tevemadar Apr 19 '23 at 21:52

3 Answers3

1

Following regexp /(?<=\[\[).*?(?=\]\])/g will do the trick. Explanation:

  • (?<=\[\[) - positive lookbehind to match anything thats starts with [[, not including it.
  • .*? - match anything between (including empty string).
  • (?=\]\]) - positive lookahead to match anything thats ends with ]], not including it.

Here is an example:

const text = `Hi [[recipient]],
Here are your login credentials:
Username: [[username]]
Password: [[password]]`

const matchedParams = text.match(/(?<=\[\[).*?(?=\]\])/g)
console.log(matchedParams)
ulou
  • 5,542
  • 5
  • 37
  • 47
0

We can simply use lookarounds:

const input = `Hi [[recipient]],

Here are your login credentials:

Username: [[username]]

Password: [[password]]`

const matches = [...(input.match(/(?<=\[\[).*?(?=\]\])/g) || [])]
console.log(matches)

In JavaScript it's also simple to replace the values if you need to with the built-in replace function:

const input = `Hi [[recipient]],

Here are your login credentials:

Username: [[username]]

Password: [[password]]`

function replace(input, map) {
  return input.replace(/\[\[.*?\]\]/g, val => map[val.slice(2, -2)])
}

console.log(replace(input, { recipient: "Bob", username: "b00", password: "12345" }))
code
  • 5,690
  • 4
  • 17
  • 39
0

You can use a simple regex and map with slice remove [[ and ]] from results

const grabVals = (str) => (str.match(/\[\[(\w+)\]\]/g) || []).map(el => el.slice(2, -2));
const str = `Hi [[recipient]],

Here are your login credentials:

Username: [[username]]

Password: [[password]]`;
const vals = grabVals(str);

console.log(vals);

Regex explaination:

\[\[ - matches string "[["

(\w+) - matches any word characters

\]\] - matches string "]]"

protob
  • 3,317
  • 1
  • 8
  • 19