Я хотел бы умножить все числа в строках, содержащих «buyPrice:» с определенным значением.Числа с умножением на строки, соответствующие регулярному выражению
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
я обнаружил кусок кода (см ниже), который возвращает «buyPrice: NaN» вместо «buyPrice: 1000» и т.д., если я, например, использовать множитель 2. Я был бы признателен за помощь!
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>
Использовать 'input.value = input.value.replace (/ buyPrice: (\ d +)/g, function (match, group1) { return 'buyPrice:' + (parseInt (group1, 10) * window.factor) }) ' –
Это сработало! Спасибо огромное! Очень ценю помощь. – AvidLearner
Рад, что это сработало для вас. Пожалуйста, подумайте о принятии ответа (см. [Как принять ответы SO]] (http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work)). –