Skip to content

Instantly share code, notes, and snippets.

@upning
Created April 4, 2022 15:58
Show Gist options
  • Save upning/95cad111cefd83208f96fd392239e2d9 to your computer and use it in GitHub Desktop.
Save upning/95cad111cefd83208f96fd392239e2d9 to your computer and use it in GitHub Desktop.
[Remove space from a string] Using REGEX in JS #regex
/// https://stackoverflow.com/questions/5963182/how-to-remove-spaces-from-a-string-using-javascript
str = str.replace(/\s+/g, '');
// \s is the regex for "whitespace", and g is the "global" flag, meaning match ALL \s (whitespaces).
// A great explanation for + can be found https://stackoverflow.com/questions/5964373/is-there-a-difference-between-s-g-and-s-g
// Alternative
var a = b = " /var/www/site/Brand new document.docx ";
console.log( a.split(' ').join('') );
console.log( b.replace( /\s/g, '') );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment