簡體   English   中英

如何在 Jquery/Javascript 中獲取最后兩個括號之間的數據

[英]How to get the data between last two brackets in Jquery/Javascript

幫助我在最后一個括號之間獲取數據。 下面是兩個示例字符串:

This (is) jquery (Application) and Output: Application   
This (is) jquery (Application) with bootstrap (css) and Output: css

我嘗試了下面的代碼

var str = "This (is) jquery (Application)"; pos = str.indexOf("(") + 1; 
str = str.slice(pos, str.lastIndexOf(")"));

我得到了第一個(和最后一個)之間的值。 如何獲得最后兩個括號之間的值?

為什么不取()的最后一個索引

pos = str.lastIndexOf("(") + 1; str = str.slice(pos, str.lastIndexOf(")"));

JS小提琴在這里

您可以使用 RegEx 獲取匹配字符串並使用最后一個作為結果:

Regex.Matches("(?<=\()(.*?)(?=\))", "This (is) jquery (Application)")

它將返回isApplication 然后你可以把第二個作為結果。

演示: https://regex101.com/r/5TJqAd/1

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM