Skip to content

Instantly share code, notes, and snippets.

@musubu
Created September 11, 2012 03:26
Show Gist options
  • Save musubu/3695733 to your computer and use it in GitHub Desktop.
Save musubu/3695733 to your computer and use it in GitHub Desktop.
Trim in JavaScript
// 全角スペースがある場合は半角スペースに置換されます
trim: function(string, callback){
var callbackExists = false;
if (callback && typeof(callback) === 'function') callbackExists = true;
try {
string = string.replace(/ /g, ' ').replace(/(^\s+)|(\s+$)/g, "");
if (callbackExists) callback(null, string);
else return string;
} catch (e) {
if (callbackExists) callback(e, null);
throw e;
}
}
@musubu
Copy link
Author

musubu commented Nov 1, 2012

コールバックが与えられなかった場合にはトリムした値をそのまま返すよう変更。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment