-5

I have a string in javascript. I need to pick all strings between "{" and "}".

Example:

QUO-{MM/YYYY}-{SERVICEGROUP}

Here i need to grab strings between {}.

Oleksandr T.
  • 76,493
  • 17
  • 173
  • 144
Vasu
  • 129
  • 2
  • 9
  • 6
    Okay. Once you've given it a try, if you get stuck, post a question showing what you've tried and asking why it isn't working. More: [*How do I ask a good question?*](/help/how-to-ask) – T.J. Crowder Oct 14 '15 at 12:47
  • try using regex.. something like this `{(.*)}` should do. – Sachin Oct 14 '15 at 12:52
  • can there be escaped `{` and `}` characters inside a `{}` group? Also, how robust do you want your parser to be (e.g. should it crash on `{SERVICEGROUP}}`, ignore the extra `}`, include it…)? – Touffy Oct 14 '15 at 12:55
  • Possible duplicate of [Regular Expression to get a string between parentheses in Javascript](http://stackoverflow.com/questions/17779744/regular-expression-to-get-a-string-between-parentheses-in-javascript) – Rolwin Crasta Oct 14 '15 at 12:57
  • @sachin, thanks a lot – Vasu Oct 14 '15 at 12:57

1 Answers1

1

Use regular expression /{(.*?)}/g:

var re = new RegExp('{(.*?)}', 'g');
result = re.exec('QUO-{MM/YYYY}-{SERVICEGROUP}');

Will output: ["{MM/YYYY}", "MM/YYYY"]

Edit:

'QUO-{MM/YYYY}-{SERVICEGROUP}'.match(/{(.*?)}/g);

Will output: ["{MM/YYYY}", "{SERVICEGROUP}"]

dr.dimitru
  • 2,645
  • 1
  • 27
  • 36