Skip to content

Instantly share code, notes, and snippets.

@ruzz311
Last active June 23, 2023 12:49
Show Gist options
  • Save ruzz311/073ae57c471a97b6cefa1a39db5d3a69 to your computer and use it in GitHub Desktop.
Save ruzz311/073ae57c471a97b6cefa1a39db5d3a69 to your computer and use it in GitHub Desktop.
Regex example to remove spaces between tags (html, xml, etc.)
// For regex testing https://regex101.com/r/dOjkeJ/2
let myStr = `<something>
<xml-like>
<example><p>with lots of spaces</p></example>
and \n characters
<xml-like>
</something>`;
// spaces:
myStr.replace(new RegExp('\>[ ]+\<', 'g'), '><');
// matches "\n" "\t" and " " (space)
myStr.replace(new RegExp('\>[\s]+\<', 'g'), '><');
@cybertrapped
Copy link

cybertrapped commented Jun 23, 2023

This one worked for me:
myStr.replace(new RegExp('>[ ]+<', 'g'), '><');

This one did not worked for me:
myStr.replace(new RegExp('>[\s]+<', 'g'), '><');

Right now, the one that works for \r,\n,\t is with double backward slashes as in:
content.replace(/\t|\r|\n|./g,"");

Thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment