【问题标题】:how to find number and calculate (change original number) within document [closed]如何在文档中查找数字并计算(更改原始数字)[关闭]
【发布时间】:2015-02-11 16:10:36
【问题描述】:

我有一个带有数字的文本:

width: 32px;
height: 11px;
top: 102px;
left: 36px;

width: 32px;
height: 11px;
top: 102px;
left: 104px;

width: 32px;
height: 11px;
top: 102px;
left: 104px;

我想在 div/file/document 中找到每个数字并将其乘以 0,25 = 将大小缩小 25%。最后将其四舍五入为整数(可选)。我想走捷径完成重复性任务,但卡住了 ;)

我会接受任何 javascript 或 python ..或任何快速的解决方案。我到目前为止:http://codepen.io/anon/pen/pvyWqb 谢谢。

【问题讨论】:

  • 您能提供到目前为止的尝试吗? SO 不会免费写代码。
  • 请在您的问题中添加minimal example,并附上问题的准确描述(例如错误回溯、输入以及预期和实际输出)。跨度>
  • 我回滚了您上次在您的问题中添加了@wolffer-east 答案的编辑。您不是新用户(尽管您从未使用过Introductory Tour),您应该知道 SO 是一个问答的网站。

标签: javascript python regex replace sequences


【解决方案1】:

您可以使用以下功能:

function return_quarter(str) {
  return str.replace(/([0-9]+)/g,function (number) {
    return Math.round(number * .25);
  });
}

return_quarter('width: 32px; height: 11px; top: 102px; left: 36px;') 返回"width: 8px; height: 3px; top: 26px; left: 9px;"

编辑:

查看您的代码,您需要用括号捕捉所有内容,而不是类本身。嵌套替换它是!

function return_quarter(str) {
  return str.replace(/(\{[^}]*\})/g,function (innards) {
    console.log(innards)
    return innards.replace(/([0-9]+)/g,function (number) {
      return Math.round(number * .25);
    });
  });
}

有机会就试试吧

【讨论】:

  • oop,忘记删除控制台日志。用这个就拿出来
  • 括号..你太棒了!谢谢
猜你喜欢
  • 2011-03-09
  • 2019-02-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-08-26
  • 1970-01-01
  • 2020-04-29
  • 2020-08-17
相关资源
最近更新 更多