2

So, I recently found this example on trimming whitespace, but I've found that it also affects strings in code. For instance, say I'm doing a lesson on string comparison, and to demonstrate that "Hello World!" and "Hello World!" are different, I need the code compression to not have any effect on those two strings.

I'm using the whitespace compression so that people with different formatting styles won't be punished for using something that I don't use. For instance, I like to format my functions like this:

function foo(){
    return 0;
};

While others may format it like this:

function foo()
{
    return 0;
};

So I use whitespace compression around punctuation to make sure it always comes out the same, but I don't want it to affect anything within a string. Is there a way to add exceptions in JavaScript's replace() function?

Community
  • 1
  • 1
Kelvin Shadewing
  • 303
  • 2
  • 16

2 Answers2

1

UPDATE:

check this jsfiddle

var str='dfgdfg   fdgfd fd gfd  g print("Hello      World!"); sadfds                dsfgsgdf' 
var regex=/(?:(".*"))|(\s+)/g;
var newStr=str.replace(regex, '$1 ');
console.log(newStr);
console.log(str);

In this code it will process everything except the quoted strings

to play with the code more comfortably you can see how the regex is working : https://regex101.com/r/tG5qH2/1

Community
  • 1
  • 1
Yehia Awad
  • 2,898
  • 1
  • 20
  • 31
  • I tried that, but it seems to delete the sections with strings. For instance, `print("Hello World!");` just becomes `print( );`. – Kelvin Shadewing Mar 09 '16 at 16:22
  • @KelvinShadewing I found where the bug was you can have another look at it ) – Yehia Awad Mar 09 '16 at 17:22
  • I think it's important to remember that since he is dealing with different users' formatting, it is possible that you can get strings wrapped in single quotes as well. Not just that, but then when you add the collection of bot " and ' into the regexp, the greediness of the regexp will cause it to provide errant behavior. – Corvus Crypto Mar 09 '16 at 17:28
  • OP didn't ask for both of them and even if he did I don't think it is a problem ) – Yehia Awad Mar 09 '16 at 17:32
  • No, I didn't, but that's because I didn't even take that into account. I think that's a good thing to watch out for, especially since some people like to alternate using `"` and `'` instead of using `\"`. – Kelvin Shadewing Mar 11 '16 at 18:59
0

I made a jsfiddle here: https://jsfiddle.net/cuywha8t/2/

var stringSplitRegExp = /(".+?"|'.+?')/g;
var whitespaceRegExp = /\s+\{/g;
var whitespaceReplacement = "{"

var exampleCode = `var str = "test test test" + 'asdasd "sd"';\n`+
                  `var test2 = function()\n{\nconsole.log("This is a string with 'single quotes'")\n}\n`+
                  `console.log('this is a string with "double quotes"')`;

console.log(exampleCode)

var separatedStrings =(exampleCode.split(stringSplitRegExp))

for(var i = 0; i < separatedStrings.length; i++){
    if (i%2 === 1){
    continue;
  }
  var oldString = separatedStrings[i];
  separatedStrings[i] = oldString.replace(whitespaceRegExp, whitespaceReplacement)
}

console.log(separatedStrings.join(""))

I believe this is what you are looking for. it handles cases where a string contains the double quotes, etc. without modifying. This example just does the formatting of the curly-braces as you mentioned in your post.

Basically, the behavior of split allows the inclusion of the splitter in the array. And since you know the split is always between two non-string elements you can leverage this by looping over and modifying only every even-indexed array element.

If you want to do general whitespace replacement you can of course modify the regexp or do multiple passes, etc.

Corvus Crypto
  • 2,141
  • 1
  • 13
  • 14