Regex 使用正则表达式在Visual Studio中更改文本
您可能知道,Visual Studio的查找和替换功能允许我们使用正则表达式,但我不知道如何更改如下内容:Regex 使用正则表达式在Visual Studio中更改文本,regex,visual-studio-2008,Regex,Visual Studio 2008,您可能知道,Visual Studio的查找和替换功能允许我们使用正则表达式,但我不知道如何更改如下内容: Math.round((document.getElementById('selectedproductPrixdock').value*document.getElementById('addxdock').value)*100)/100 对于这一点: (Math.round((document.getElementById('selectedproductPrixdock').val
Math.round((document.getElementById('selectedproductPrixdock').value*document.getElementById('addxdock').value)*100)/100
对于这一点:
(Math.round((document.getElementById('selectedproductPrixdock').value*document.getElementById('addxdock').value)*100)/100).toFixed(2)
页面上有太多这样的代码,一个接一个地修改它们是一件很麻烦的事
提前感谢..这看起来不是正则表达式的很好候选,因为它们用于查找/替换模式。从这篇文章中得出一个模式可能是浪费时间 我会这样做:
string s = "...some text...";
string toBeReplaced = "Math.round((document.getElementById('selectedproductPrixdock').value*document.getElementById('addxdock').value)*100)/100";
string replacement = "(" + toBeReplaced + ").toFixed(2)";
string result = s.Replace(toBeReplaced, replacement);
编辑:
在重新阅读你的问题后,了解每个人的ID会使问题变得更加困难。下面是一个应该有效的正则表达式:
string s = "...some text...";
string result = Regex.Replace(s, @"Math\.round\(\(document\.getElementById\('.+'\)\.value*document\.getElementById\('.+'\).value\)*100\)/100", "($0).toFixed(2)");
你想要匹配的东西有多相似 如果它们除了内部参数外都是相同的,那么
s/Math.round\(\(document.getElementById\('(.*?)'\).value*document.getElementById\('(.*?)'\).value\)*100\)\/100/\(Math.round\(\(document.getElementById\('$1'\).value*document.getElementById\('$2'\).value\)*100\)\/100\).toFixed\(2\)/g
将$1和$2替换为VS用来填充反向引用的任何东西。我认为你对regex的要求太高了
在相关新闻中,如果你没有把五十条指令打包成一行,那么你可能会有更容易的时间重构。我们需要多一点上下文——第一行是整行,还是在中间行?其他目标行与此行有多相似?您好,内部部分有时可能不同,但正则表达式不应该关心内部部分,我只需要在开始处添加
(
,然后在给定字符串的末尾添加)。toFixed(2)
。