14

Here is a test:

console.log(" Golden Katana of the Unflinching Dawn                                    ".replace(/\s+/g,""))

I want to remove the extra spaces at the end of the string but it removes every space in the string, so how could I remove just the extra spaces at the end and just keep Golden Katana of the Unflinching Dawn?

hexacyanide
  • 88,222
  • 31
  • 159
  • 162
wateraura
  • 331
  • 1
  • 2
  • 8
  • Please have a look here: http://stackoverflow.com/questions/498970/how-do-i-trim-a-string-in-javascript – Ynhockey Jul 21 '13 at 06:39

2 Answers2

28

You can use str.trim() for this case. It removes the leading and trailing whitespace from a string. The regular expression str.replace(/^\s+|\s+$/g,'') will alternatively do the same thing.

hexacyanide
  • 88,222
  • 31
  • 159
  • 162
8

try doing

trimmedstr = str.replace(/\s+$/, '');

or maybe

.replace(/ +$/, "");
No Idea For Name
  • 11,411
  • 10
  • 42
  • 70