Posted By

splorp on 01/25/07


Tagged

format delete trim whitespace String text formatting leading trailing


Versions (?)

Who likes this?

2 people have marked this snippet as a favorite

ebukva
artificialkid


Trim Leading & Trailing Whitespace


 / Published in: JavaScript
 

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

  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  

You need to login to post a comment.