We Recommend

Pro JavaScript Techniques Pro JavaScript Techniques
Pro JavaScript Techniques is the ultimate JavaScript book for the modern web developer. It provides everything you need to know about modern JavaScript, and shows what JavaScript can do for your web sites. This book doesn't waste any time looking at things you already know, like basic syntax and structures.


Posted By

splorp on 01/25/07


Tagged

format delete trim whitespace String text formatting leading trailing


Versions (?)


Who likes this?

1 person has marked this snippet as a favorite

ebukva


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.