Regex删除某些字符周围不需要的空格
本文关键字:不需要 空格 周围 字符 删除 Regex | 更新日期: 2023-09-27 18:00:51
我正在尝试从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
为什么要重新发明轮子?检查http://compressorrater.thruhere.net/出来
要删除某些字符(如[=*/+-]
(周围的空白,请使用以下正则表达式:
text = Regex.Replace(text, @"'s*([=*/+-])'s*", "$1");
在[]
之间,-
字符必须是第一个或最后一个,以避免其含义为一系列字符
测试代码示例
text = Regex.Replace(text, @(" (=|'+|-) ", "$1");