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
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
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
like this:
var s = "my cool example string";
var s_no_space = s.replace(/ /g, '');
You can use regular expressions:
var str = "a string with spaces";
var nospacesStr = str.replace(/\s/g, ""); // "astringwithspaces"