Last active
June 23, 2023 12:49
-
-
Save ruzz311/073ae57c471a97b6cefa1a39db5d3a69 to your computer and use it in GitHub Desktop.
Regex example to remove spaces between tags (html, xml, etc.)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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'), '><'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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