Skip to content

Instantly share code, notes, and snippets.

@FrankFang
Forked from randika/gist:299049
Created September 6, 2012 12:22
Show Gist options
  • Save FrankFang/3655713 to your computer and use it in GitHub Desktop.
Save FrankFang/3655713 to your computer and use it in GitHub Desktop.
Javascript trim with Regular Expressions
<script type="text/javascript">
//Javascript trim() example
String.prototype.trim = function() { return this.replace(/^\s+|\s+$/g, ""); };
//Usage:
var str = " foo bar ";
alert("Original string: '" + str + "'");
str = str.trim();
alert("Trimmed string: '" + str + "'");
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment