Created
September 11, 2012 03:26
-
-
Save musubu/3695733 to your computer and use it in GitHub Desktop.
Trim in JavaScript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 全角スペースがある場合は半角スペースに置換されます | |
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; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
コールバックが与えられなかった場合にはトリムした値をそのまま返すよう変更。