2

I have a string like

{{token1}}/{{token2}}

I want to match {{token2}} using regex.

other possible cases that it should match are

{{token1}}/{ should match { (the last one).

{{token1}}/{{ should match {{ (the last one).

{{token1}}/{{token2 should match {{token2.

I tried /\S+$/ but it works when the string is {{token1}} {{token2}}.

Thanks in advance.

Prashant Agrawal
  • 660
  • 9
  • 24

1 Answers1

6

You can use a negative lookahead regex:

/{+[^}]*}*(?!.*{)/

(?!.*{) is negative lookahead that asserts we don't have any { ahead of the current position.

RegEx Demo

anubhava
  • 761,203
  • 64
  • 569
  • 643