엄청 간단합니다.

String.prototype.trim = function(){
    return this.replace(/^(\s| )+|(\s| )+$/g, "");
}

String.prototype.ltrim = function(){
    return this.replace(/^(\s| )+/g, "");
}

String.prototype.rtrim = function(){
    return this.replace(/(\s| )+$/g, "");
}

str = "   1234567890   ";
ltrim = str.ltrim();
rtrim = str.rtrim();
trim = str.trim();

result = "<pre>orignal = \"" + str + "\"\n";
result += "ltrim length = " + ltrim.length + ", string = \"" + ltrim + "\n";
result += "rtrim length = " + rtrim.length + ", string = \"" + rtrim + "\n";
result += "trim length = " + trim.length + ", string = \"" + trim + "\n";

document.write(result);

크리에이티브 커먼즈 라이선스
Creative Commons License
Trackback 1 And Comment 0