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?

2
  • perhaps you could tokenize all the strings, keep a map of the tokens, then adjust whitespace, then replace the tokens with the original string contents? Commented Mar 8, 2016 at 22:13
  • I don't think I've even heard of tokenizing. Commented Mar 9, 2016 at 16:22

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

5 Comments

I tried that, but it seems to delete the sections with strings. For instance, print("Hello World!"); just becomes print( );.
@KelvinShadewing I found where the bug was you can have another look at it )
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.
OP didn't ask for both of them and even if he did I don't think it is a problem )
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 \".
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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.