我正在尝试从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;
}
我的第二个表达式目前会将“ =”替换为“ =”。我想指定更多的字符和关键字,可以从两侧删除空格。
我该如何在正则表达式中搜索这个字符或关键字,然后在替换中进行反向引用?
谢谢!
\r+
或\n+
吗?我相当确定这可能会改变你的JavaScript的语义(即使它不会破坏语法)。 - LukeHa=1\r\nb=4
和a=1;b=4
是可以的,但a=1 b=4
不行。 - Alex K.