-1

I have email template that look like that:

Dear {user.firstName},<br><br> Thank you for buying {item} with us. <br><br>
Your Order ID is : <b> {{orderId}} </b>. <br><br>
Thank again for supporting us.

Below is the example of what i trying to extract from the paragraph:

1) The thing inside {} from a paragraph

user.firstName // from {user.firstName}
item // from {item}
orderId // from {{orderId}}

2) Entire {} including the thing inside from a paragraph

{user.firstName} // from {user.firstName}
{item} // from {item}
{orderId} // from {{orderId}}

For (1), I tried with /{(.*)}/g or /({)(.*)(})/g but it will still select entire {{tag}}.

Below is what i have tried for (2):

/({([a-z])\w+})|({([a-z])+})|({([A-Z])\w+})|({([A-Z])+})|({[0-9]+})/g

It can cater most of criteria, except if there is a/multiple dot in the {tag}.

Please help and thank you in advance.

Jerry
  • 1,455
  • 1
  • 18
  • 39

2 Answers2

2

You could try using the following regexp:

({([\w\.]*)})

There your matches would have two capture groups:

  1. The full match, with your braces, like {user.firstName}
  2. Whatever is inside the brances, like user.firstName

You can also check that expression here https://regex101.com/r/eXKJzs/1/

J. Pichardo
  • 3,077
  • 21
  • 37
  • OP should note this will work only if we explicitly allow only one or two brackets, since [regular expression can't handle more complex nesting](https://stackoverflow.com/questions/133601/) – patstuart Jun 01 '18 at 20:16
  • @J.Pichardo Thanks for replying. As tested, it work with my question (2), in a very simple way. Thanks again :) – Jerry Jun 02 '18 at 03:21
0

Edit 2 : based on comments received from @melpomene

var reg = /{([\w(.*\w)*]+)}/g;

var string = "{x}Dear {user.firstName},<br><br> Thank you for buying {item} with us. <br><br>Your Order ID is : <b> {{orderId}} </b>. <br><br> Thank again for supporting us.";

var result = null;
while(result = reg.exec(string))
    console.log(result[1]);
Vivek
  • 1,465
  • 14
  • 29
  • 1
    Your code fails horribly if you add a single-letter tag at the beginning of `string`: `"{x} Dear {user.firstName}, ..."` – melpomene Jun 01 '18 at 20:19
  • @melpomene it will match content inside `{` and `}` starting from `{` to first occurrence of `}`. `(?=})` means until first positive lookahead, same as `break` in looping. – Vivek Jun 01 '18 at 20:20
  • 1
    You already have a fixed `}` in your regex. `(?=})}` is redundant. – melpomene Jun 01 '18 at 20:22
  • @melpomene can you please check now? I've edited code and added example. – Vivek Jun 01 '18 at 20:29
  • `[\d\w]` is just `\w`. `\w` already includes digits. `[.*]` is nonsensical here. It matches either a literal `.` or a literal `*`. – melpomene Jun 01 '18 at 20:30
  • If you want to match everything up to the next `}`, why not just do `{([^}]+)}`? What's this obsession with `+?`? – melpomene Jun 01 '18 at 20:31
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/172274/discussion-between-vivek-and-melpomene). – Vivek Jun 01 '18 at 20:45