Pot.js で利用可能。 PotLite.js では利用できません。
正規表現 pattern にマッチした string の特定のグループを返します。
引数 string は、対象の文字列を指定します。
引数 pattern は、抽出したい表現を含む正規表現を指定します。
extract は、'foo bar'.match(/(fo+)/)[1]
のような抽出と同じです。
'foo bar'
が string、/(fo+)/
が pattern、 1
が index にあたります。
ただし、'foo bar'.match(/(b.z)/)[1]
とした場合、TypeError になるのを防ぎます。
ヒットしたかどうか test して、その後 抽出というステップを簡単にすることができます。
引数 index に数値を渡すと、そのインデックスが返ります。
マッチしなかった場合は、空文字 '' が返ります。
Pot.globalize() が適応されている場合、Pot.extract() が extract() で実行できます。
pattern にマッチした string のグループのうち index があればそれを返します。
var result = Pot.extract('foo:bar', /:(\w+)$/); debug(result); // 'bar'
var result = Pot.extract('foo:bar', /^:(\w+)/); debug(result); // ''
var result = Pot.extract('foo.html', /(foo|bar)\.([^.]+)$/, 2); debug(result); // 'html'
var result = Pot.extract('foobar', 'foo'); debug(result); // 'foo'
var result = Pot.extract('foobar', 'fo+'); debug(result); // ''