It seems that I may be making a mistake as I found regex patterns to achieve the desired replacement in PHP and C#, but when trying to apply it in JavaScript, it is not working.
For example:
text ( 4 |-4 | 1 "test" ) [ 0 50 90 ]
After cleaning, it should look like this:
text(4|-4|1 "test")[0 50 90]
In essence, I am aiming to remove all white spaces before and after brackets and the | symbol.
My current code attempts are:
// remove whitespaces around brackets []
text = text.replace(/\s*\[\s*(.*?)\s*]\s*/g, '[$1]');
// remove whitespaces around brackets ()
text = text.replace(/\s*\(\s*(.*?)\s*\)\s*/g, '($1)');
// remove all whitespaces around | (unsuccessful attempt)
// text = text.replace(/\s*\|\s*(.*?)\s*\|\s*/g, '|$1|');
// text = text.replace(/\s*|\s*/, '$1');
This approach seems overly complex.
I would appreciate assistance in identifying the regex pattern for each symbol individually.
Instead of combining all replacements into one regex pattern, I prefer to learn by having one replacement per line.