最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to match the last occurrence of a pattern using regex - Stack Overflow

programmeradmin0浏览0评论

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.

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.

Share Improve this question edited Dec 2, 2016 at 6:55 Prashant Agrawal asked Dec 2, 2016 at 6:43 Prashant AgrawalPrashant Agrawal 67010 silver badges25 bronze badges 4
  • Can you explain what you mean by "match"?. Do you want to "extract it"? – TheLostMind Commented Dec 2, 2016 at 6:48
  • @Tushar I have update the question with all possible scenarios – Prashant Agrawal Commented Dec 2, 2016 at 6:56
  • @TheLostMind I am searching this pattern in a string. – Prashant Agrawal Commented Dec 2, 2016 at 6:57
  • Does this answer your question? Find Last Occurrence of Regex Word – pilchard Commented Oct 2, 2021 at 12:12
Add a ment  | 

1 Answer 1

Reset to default 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

发布评论

评论列表(0)

  1. 暂无评论