-2

I would like to avoid spaces from a string in javascript. I would like to remove spaces not only infront and after of the string , instead I would like to remove spaces from the string (front,in between the characters and end).

Thanks in adavance

Technosavia
  • 85
  • 2
  • 12

3 Answers3

0

CODE:

var str = "String String String";
str = str.replace(/ /g,'');  // ' '  -> REMOVE ONLY ' ', NOT \n, \r, \t, and \f
console.log(str);            // '/g' -> GLOBAL or MATCH_ALL or FIND ALL ' '

Note: Change the / /g to /\s/g if you want to include \n, \r, \t, \f, and " "


OUTPUT:

StringStringString

Mathew Kurian
  • 5,949
  • 5
  • 46
  • 73
0

like this:

var s = "my cool example string";
var s_no_space = s.replace(/ /g, '');
HRÓÐÓLFR
  • 5,842
  • 5
  • 32
  • 35
0

You can use regular expressions:

var str = "a string with spaces";
var nospacesStr = str.replace(/\s/g, ""); // "astringwithspaces"
Nikita Tkachenko
  • 2,116
  • 1
  • 16
  • 23