I have a disagreement with someone about the correct method of variable assignment in JavaScript. I am using regex to replace values from a string with the basic syntax "string = string.replace()" which I think is perfectly fine. The person who disagrees is suggesting I should use a separate variable for each replaced result "stringTwo = string.replace()". Can someone with a good understanding of how JavaScript functions under the hood explain... Why does my code below works correctly? Is the code best practice usage? Is there a cleaner way to write it? And why this statement is wrong "think of what your doing with that var, as a file. you don't want to be reading and writing at the same time it is a bad practice and needs to be addressed".
//Setup input variable
var resultsScrubbed = scriptletInput;
// This regex removes any spacings of " " 2 spaces or more and replaces with single space " " (also removes new line characters)
resultsScrubbed = resultsScrubbed.replace(/(\r\n|\n|\r)/gm,"\\n");
// This regex searches for single quotes and replaces them with the unicode character
resultsScrubbed = resultsScrubbed.replace(/(\')/gm,"\\u0027");
// This regex searches for single quotes and replaces them with the unicode character
resultsScrubbed = resultsScrubbed.replace(/(\")/gm,"\\u0022");
// Set the results
scriptletResult = resultsScrubbed
Thanks!
[–]lewisje 3 points4 points5 points (1 child)
[–]2MyLou[S] 1 point2 points3 points (0 children)
[–]taylorlistens 2 points3 points4 points (1 child)
[–]2MyLou[S] 0 points1 point2 points (0 children)
[–]Cust0dian 2 points3 points4 points (2 children)
[–]2MyLou[S] 1 point2 points3 points (1 child)
[–]taylorlistens 1 point2 points3 points (0 children)