2017-05-11 5 views

答えて

2

@loretoparisiと同じですが、グループを上書きします。

str = "foo[biz][bar]" 
 
matches = /(?:\[(\w+)\])+/g.exec(str) 
 
console.log(matches.pop())

+0

それは間違いなくそれを忘れてしまった! – loretoparisi

5

ヨ括弧の間のすべての一致を得るために正規表現/\[(.*?)\]/gを使用して、もしあれば、最後の試合を取ることができます。

str = "foo[biz][bar]" 
 
matches = str.match(/\[(.*?)\]/g) 
 
if (matches.length) console.log(matches[matches.length - 1]) 
 
// based on answer above group override but without `(?:` non capturing group 
 
console.log(/(\[(\w+)\])+/g.exec(str).pop())

正規表現/\[(.*?)\]/g説明: enter image description here

正規表現/(\[(\w+)\])+/g説明:enter image description here

作成者Debuggex

+2

あなたはパターンがありませんどのように説明してもらえ作業?パターンが '/\[(.*)\]/g'のとき、なぜ私は' '[biz] [bar] ''を得るのか理解しています。 '?'は '/\ {(?*?)\]/g'の中で前の結果を2つの部分に分割するのは何でしょうか? '?'は何の "ゼロまたは1"を意味しますか? – pmichna

+1

私も説明を加えました。 – loretoparisi

関連する問題