-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

2
  • This is called normalization. Commented Feb 27, 2013 at 18:53
  • just spaces? what about tabs and newlines? Commented Feb 27, 2013 at 18:55

3 Answers 3

0

like this:

var s = "my cool example string";
var s_no_space = s.replace(/ /g, '');
Sign up to request clarification or add additional context in comments.

Comments

0

You can use regular expressions:

var str = "a string with spaces";
var nospacesStr = str.replace(/\s/g, ""); // "astringwithspaces"

Comments

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

1 Comment

Probably a little more efficient to use \s+.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.