0

String example: "{something}" or "{word: something}"

What I need to do is get 'something', so the text between two specific parts of the string, in these case {-} and {word:-} (The 'something' part can change in every string, I never know what it is).

I tried using string.find() or regex but I didn't come up with a conclusion. What's the quickest and best way to do this?

adiga
  • 34,372
  • 9
  • 61
  • 83
Giuliopime
  • 707
  • 4
  • 21

1 Answers1

2

What you need is a capture group inside a regex.

> const match = /\{([^}]+)\}/.exec('{foo}')
> match[1]
'foo'

The stuff in the parens, ([^}]+), matches any character but }, repeated at least once. The parens make it be captured; the first captured group is indexed as match[1].

9000
  • 39,899
  • 9
  • 66
  • 104