18

Can anyone help me on how to remove trailing spaces in JavaScript. I want to keep the leading spaces as is and only remove trailing spaces.
EG: ' test ' becomes ' test'. Seems like pretty simple but I can't figure it out.

PS: I am pretty sure I can't be the first one to ask this but I can't find an answer in SO. Also, I am looking for JavaScript solution. I am not using jQuery.

4
  • 1
    Just install trim via npm. It was installed nearly 10,000 times yesterday. After all, what could go wrong? Haha, I must laugh so I don't cry. Commented Jun 16, 2016 at 16:27
  • @MichaelPlotke Why do you think he's using node.js? Commented Jun 16, 2016 at 16:29
  • There are lots of questions about removing both leading and trailing whitespace, but I can't find a duplicate that's just about removing trailing spaces. Commented Jun 16, 2016 at 16:30
  • 1
    why the downvotes? Commented Jun 16, 2016 at 16:40

6 Answers 6

30

Use String#replace with regex /\s+$/ and replacing text as empty string.

string.replace(/\s+$/, '')

console.log(
  '-----' + '    test    '.replace(/\s+$/, '') + '-----'
)

Sign up to request clarification or add additional context in comments.

3 Comments

@frankies \s matches all types of whitespace.
@Barmar Yep. If people want to just replace spaces (not linebreaks and such), they should use string.replace(/ +$/, '').
Do the forward slashes at the beginning and end of the first argument indicate that the first argument is a regular expression?
3

Update 2019

String trim methods have been added to ECMAScript spec in 2019 with wide browser support

You can use like this:

" test ".trimEnd() // " test"

Further Reading

Comments

2
"    test    ".replace(/\s+$/g, '');

1 Comment

No point in having the g flag with this regex as \s+ already covers all the trailing spaces
2

Regex is worth knowing. But in case you don't, and don't want to use code you're not comfortable with, nor dive down that rabbit-hole for your current issue, here's a simple manual workaround:

/**
 * @example
 * trimEndSpaces('test   \n   '); // Returns: 'test   \n'
 * @param {string} str 
 * @returns {string}
 */
function trimEndSpaces(str) {
  let i = str.length - 1;
  // decrement index while character is a space
  while (str[i] === ' ') {
    i--;
  }
  // return string from 0 to non-space character
  return str.slice(0, i + 1);
}

Comments

0

use trimRight()

var x ="   test   "

x.trimRight()

2 Comments

And browser support of that non-standard method is bad.
Thank you epascarello I was not aware of that i just verified on chrome and firefox 38 Its not working in I.E 11
0

There is a way by which you can create a regex inside the replace method like str.replace(/\s+$/g,'') will remove all trailing spaces.

5 Comments

You don't need the g modifier, since something that's anchored can't have multiple matches.
Yup you are right , my bad because this pattern will be unique so no need of g[global]
Did you read the red warning at the beginning of the page you linked to?
Yes... that is why i just gave the reference not posted the method.
I think posting the link seems like a recommendation to use it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.