面向对象的特性-为String类型的变量扩展一个replaceAll()函数

————————————————————————————

<script type="text/javascript">        
            //按钮的click事件监听函数
            function startReplace(){
                var mytxt = document.getElementById('mytxt').value;
                var s1 = document.getElementById('s1').value;
                var s2 = document.getElementById('s2').value;
                var str = mytxt.replaceAll(s1,s2);
                alert('替换以后的字符为:' + str);
            }
            String.prototype.replaceAll = function(src,tgt){
                return this.replace(new RegExp(src,'ig'),tgt);
            };                
</script>

——————————————————————————————

<body style="text-align:center">
        源字符:<input type="text" name="mytxt" id="mytxt"/><br><br>
        替换字符:<input type="text" name="s1" id="s1"/><br><br>
        目标字符:<input type="text" name="s2" id="s2"/>
        <br/><br/>
        <input type="button" value="替换" onclick="startReplace()"/>
    </body>

————————————————————————————————

posted @ 2016-09-30 14:57  承载梦想-韩旭明  阅读(211)  评论(0编辑  收藏  举报