gpt4 book ai didi

c# - 正则表达式删除某些字符周围不需要的空格

转载 作者:行者123 更新时间:2023-11-30 14:39:21 25 4
gpt4 key购买 nike

我正在尝试从 JavaScript 文件中删除一些不需要的空格,并在将文件发送到客户端之前使用 C# 和 Regex 组合文件。我有一个 JavascriptHandler 来处理 .js 文件,它工作正常。这是我用来“打包”JavaScript 的函数。

private string PackJs(string file)
{
string text = System.IO.File.ReadAllText(JSFolder + file);

//replace any combination of unwanted whitespace with a single space
text = Regex.Replace(text, @"[\r\n\s]+", " ");

//Can I get this to match +, =, -, etc?
text = Regex.Replace(text, @"( [=] )", "=");

//more regular expressions here, when I get round to it

return text;
}

我的第二个表达式目前将用“=”替换“=”。我想指定更多字符和关键字,可以从两边删除空格。

我如何在正则表达式中搜索它,然后在替换中向后引用该字符或关键字?

谢谢,

最佳答案

[] 中放置你的角色

var input = "c = a + b";
var result = Regex.Replace(input, @"\s([=+])\s", "$1");

结果将是:c=a+b

关于c# - 正则表达式删除某些字符周围不需要的空格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6536195/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com