2007/08/31 12:12
[JS] 정규식을 이용하여 ltrim, rtrim, trim 구현하기 작은 팁2007/08/31 12:12
엄청 간단합니다.
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, "");
}
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);
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);
'작은 팁' 카테고리의 다른 글
| EditPlus 에서 monaco(mac 기본 폰트)사용하기. (XP에서 안티알리아싱(Anti-Aliasing 설정) (2) | 2007/10/01 |
|---|---|
| [CSS] IE6 에서 CSS만으로(핵안쓰고) position:fixed 하기 (2) | 2007/08/31 |
| [JS] 정규식을 이용하여 ltrim, rtrim, trim 구현하기 (0) | 2007/08/31 |
| [JS] timestamp 를 date 형식으로 보여주기(as like php date()) (0) | 2007/08/08 |
| [JS] HTML Attribute Parser (0) | 2007/07/13 |
| [JS] OS 알아내기 (with js & jQuery) (0) | 2007/07/10 |
