Trim Leading & Trailing Whitespace

/ Published in: JavaScript
Save to your folder(s)

Trims leading and trailing spaces from a string value and returns the result as a new string.

Copy this code and paste it in your HTML
  1. function trimWhitespace(value) {
  2. var result = value.match(/^\s*(.*\S)\s*$/);
  3. if (result !== null && result.length === 2)
  4. return result[1];
  5. else
  6. return value;
  7. };

Report this snippet


RSS Icon Subscribe to comments

You need to login to post a comment.