热门关键字:
jquery > jquery教程 > javascript > javascript常用的String对象扩展

javascript常用的String对象扩展

979
作者:管理员
发布时间:2015/5/26 13:16:45
评论数:0
转载请自觉注明原文:http://www.jq-school.com/Show.aspx?id=604

javascript常用的String对象扩展

<script type="text/javascript">
    if(typeof String.prototype.ltrim=='undefined'){
        String.prototype.ltrim = function(){
            var s = this;
            s = s.replace(/^\s*/g, '');
            return s;
        }
    }

    if(typeof String.prototype.rtrim=='undefined'){
        String.prototype.rtrim = function(){
            var s = this;
            s = s.replace(/\s*$/g, '');
            return s;
        }
    }

    if(typeof String.prototype.trim=='undefined'){
        String.prototype.trim = function(){
            return this.ltrim().rtrim();
        }
    }

    if(typeof String.prototype.htmlEncode=='undefined'){
        String.prototype.htmlEncode = function(encodeNewLine){//encodeNewLine:是否encode换行符
            var s = this;
            s = s.replace(/&/g, '&amp;');
            s = s.replace(/</g, '&lt;');
            s = s.replace(/>/g, '&gt;');
            s = s.replace(/'/g, '&quot;');
            if(encodeNewLine){
                s = s.replace(/\r\n/g, '<br />');
                s = s.replace(/\r/g, '<br />');
                s = s.replace(/\n/g, '<br />');
            }
            return s;
        }
    }
</script>





如果您觉得本文的内容对您的学习有所帮助:支付鼓励



关键字:javascript String 正则表达式 prototype function typeof undefined replace
友荐云推荐