반응형

String removeTags(input)

HTML tag부분을 없애준다
function removeTags(input) {
    return input.replace(/<[^>]+>/g, ""); 
};
example>
var str = "<b>Tmax</b> <i>soft</i>";
document.write(str +"<br>");
document.write(removeTags(str));
result>
Tmax soft
Tmax soft

String String.trim()

문자열의 앞뒤 공백을 없애준다.
String.prototype.trim = function() {
    return this.replace(/^\s+|\s+$/g, ''); 
};
example>
var str = "         untrimed string            ";
document.write("========" + str+ "==============<br>");
document.write("========" + str.trim() + "==============");
result>
======== untrimed string ==============
========untrimed string==============

String String.capitalize()

단어의 첫 글자를 대문자로 바꿔준다.
String.prototype.capitalize = function() {
    return this.replace(/\b([a-z])/g, function($1){
        return $1.toUpperCase();
    }) ;  
};
example>
var str = "korea first world best";
document.write(str.capitalize());
result>
Korea First World Best

String number_format(input)

입력된 숫자를 ,를 찍은 형태로 돌려준다
function number_format(input){
    var input = String(input);
    var reg = /(\-?\d+)(\d{3})($|\.\d+)/;
    if(reg.test(input)){
        return input.replace(reg, function(str, p1,p2,p3){
                return number_format(p1) + "," + p2 + "" + p3;
            }    
        );
    }else{
        return input;
    }
}
example>
document.write(number_format(1234562.12) + "<br>");
document.write(number_format("-9876543.21987")+ "<br>");
document.write(number_format("-123456789.12")+ "<br>");
result>
1,234,562.12
-9,876,543.21987
-123,456,789.12


출처 - http://cafe.naver.com/q69.cafe?iframe_url=/ArticleRead.nhn%3Farticleid=115820&
반응형

+ Recent posts