So I need to select all spaces not between quotes and delete them, I am using regex in Javascript notation.
For example:
" Test Test " ab c " Test" "Test " "Test" "T e s t"
becomes
" Test Test "abc" Test""Test ""Test""T e s t"
CodePudding user response:
Replace "quotes or space" with "the same thing or nothing":
input = `" Test Test " ab c " Test" "Test " "Test" "T e s t"`
result = input.replace(/(".*?")|( )/g, (_, $1, $2) => $1 || '')
console.log(result)
CodePudding user response:
We can try a regex replacement approach with a callback function:
var input = '" Test Test " ab c " Test" "Test " "Test" "T e s t"';
var output = input.replace(/".*?"|[^"] /g, (x) => x.match(/^".*"$/) ? x : x.replace(/\s /g, ""));
console.log(output);
The strategy here is to match, alternatively, either a term in double quotes, or any other content not involving a doubly quoted term. The case of the former, we do no substitution. In the case of the latter, we remove all whitespace characters.