2016-11-18 4 views
1

現在のところ、正規表現を使用してSet 1 Total Games Over/Under 9.5というセット文字列を並べ替えて、Under/Over N Giochi Set 1と読み替えるようにしています。現在、私は、データ出力に次の使用:正規表現を使用した後のJavaScriptの非構造化代入

let marketLabel = 'Set 1 Total Games Over/Under 9.5'; 
match = regexUnderOver.exec(marketLabel); 
browserReturn = match[3] + '/' + match[2] + ' ' + match[4] + ' Giochi Set ' + match[1]; 

しかし、私はbrowserReturn変数に割り当てる前にデータを正しく注文する分割代入を使用することを好むだろう。私はMDNの規則に従うように努力しましたが、それは私には意味がありません。あなたが私が投稿した例を使って私に見せてもらえれば幸いです。以下の完全なコードは:

let marketLabel = 'Set 1 Total Games Over/Under 9.5'; 
const regexUnderOver = /^Set ([0-9.]+) Total Games (Over)\/(Under) ([0-9.]+)$/; 
match = regexUnderOver.exec(marketLabel); 
browserReturn = match[3] + '/' + match[2] + ' ' + match[4] + ' Giochi Set ' + match[1]; 
return browserReturn; 
+1

です 'marketLabel.replace(/^Setを返すこと([0-9。] +)$ /、$ 3/$ 2 $ 4ジオッチセット$ 1 ') 'あなたはお探しですか? –

+0

ご不明な点がございましたら、ご容赦ください。これは、 'browserReturn = match [3] + '/' + match [2] + '' + match [4] + 'Giochi Set' + match [1];特に' match'配列を破壊する必要があります。 – DaveDavidson

+0

最終的には、正確に何を持っておきたいですか? 'match'はすでにキャプチャされたすべてのテキストを含む配列です。インデックス経由で簡単にアクセスできます。 –

答えて

2

それはあなたが

let marketLabel = 'Set 1 Total Games Over/Under 9.5'; 
 
const regexUnderOver = /^Set ([0-9.]+) Total Games (Over)\/(Under) ([0-9.]+)$/; 
 
let [fullmatch, firstNum, over, under, lastNum] = regexUnderOver.exec(marketLabel); 
 
let browserReturn = `${under}/${over} ${lastNum} Giochi Set ${firstNum}`; 
 
console.log(browserReturn);

[fullmatch, firstNum, over, under, lastNum] = regexUnderOver.exec(marketLabel)に関心があるしたいのようになります。

  • fullmatch - 全試合を
  • firstNum - キャプチャグループ1つの内容
  • over - キャプチャグループ2つの内容
  • under - キャプチャグループ3つの内容
  • lastNum - キャプチャグループ4つの内容
関連する問題