12

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
  • 82,699
  • 31
  • 154
  • 158
wateraura
  • 191
  • 1
  • 1
  • 7
  • 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

25

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
  • 82,699
  • 31
  • 154
  • 158
8

try doing

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

or maybe

.replace(/ +$/, "");
No Idea For Name
  • 11,127
  • 10
  • 38
  • 63