2016-11-04 9 views
0

"buyPrice:"を含む行のすべての数字に一定の値を掛けたいと思います。代わりに「buyPrice:1000」など、私は例えば、私は助けをいただければ幸い2の乗数を使用している場合:正規表現にマッチした行に数字を掛ける

shops: 
blocks: 
    name: "&9&lBlocks (page %page%)" 
    items: 
    1: 
     type: item 
     item: 
     material: GRASS 
     quantity: 64 
     buyPrice: 500 
     sellPrice: 50 
     slot: 0 
    2: 
     type: item 
     item: 
     material: DIRT 
     quantity: 64 
     buyPrice: 500 
     sellPrice: 30 
     slot: 1 
    3: 
     type: item 
     item: 
     material: GRAVEL 
     quantity: 64 
     buyPrice: 500 
     sellPrice: 50 
     slot: 2 

私はコードの一部(下記参照)「はNaN buyPrice」を返すことを発見しました!あなたが提供されているコードで

addEventListener('load', function() { 
 
document.getElementById('replace').addEventListener('click', function() { 
 
    window.factor = parseInt(prompt('Which factor should the values be multiplied with?', 1)); 
 
    if (factor) { 
 
     var input = document.getElementById('textinput'); 
 
     input.value = input.value.replace(/sellPrice: [0-9]+/g, function(match) { return 'sellPrice: ' + (parseInt(match, 10) * window.factor); }); 
 
    } 
 
}); 
 
});
<button id="replace">Multiply px values</button> 
 
<textarea style="width:100%;height:2000px;" id="textinput"></textarea>

+0

'buyPrice:' +(parseInt(group1、10)* window.factorを返すと、input.value = input.value.replace(/ buyPrice:(dd +)/ g、function(match、group1){ ); }) ' –

+0

それは働いた!どうもありがとうございます!本当に助けに感謝します。 – AvidLearner

+0

それはあなたのために働いてうれしい。回答を受け入れることを検討してください([回答を受け入れる方法](http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work)を参照)。 –

答えて

0

あなただけの数に数字列を変換する必要がありながら、全体マッチしたテキストは数として解析されます。したがって、括弧付き数字マッチング部を囲み、匿名メソッドの2番目の引数を渡す:

ここ
input.value = input.value.replace(/buyPrice: (\d+)/g, function(match, group1) { 
    return 'buyPrice: ' + (parseInt(group1, 10) * window.factor); 
}); 

(\d+)グループ1に1+数字を捕捉し、この値は、group1引数を介して利用できるようになります。

関連する問題