I’m trying to remove some unwanted whitespace from JavaScript files, and combine files using C# and Regex, before they are sent to the client. I have a JavascriptHandler to handle the .js files, which works fine. This is the function that I’m using to “pack” the 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;
}
My second expression currently will replace ” = ” with “=”. I’d like to specify more characters and keywords that can have the spaces removed from either side.
How do I search for this in the regular expression, then back-reference that character or keyword in the replacement?
Thanks,
Inside
[]put your charactersResult would be:
c=a+b