3

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
  • 642
  • 8
  • 23

1 Answers1

4

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
  • 713,503
  • 59
  • 514
  • 593