replace()方法3

2017-06-16 14:51:00
admin
原创
486
<!DOCTYPE html>
<html>
<head>
    <title>String Type Pattern Matching Example</title>
    <script type="text/javascript">
    
        var text = "cat, bat, sat, fat"; 
        var pattern = /.at/;
        
        var matches = text.match(pattern);        
        alert(matches.index);        //0
        alert(matches[0]);           //"cat"
        alert(pattern.lastIndex);    //0

        var pos = text.search(/at/);
        alert(pos);   //1

        var result = text.replace("at", "ond");
        alert(result);    //"cond, bat, sat, fat"

        result = text.replace(/at/g, "ond");
        alert(result);    //"cond, bond, sond, fond"

        result = text.replace(/(.at)/g, "word ($1)");
        alert(result);    //word (cat), word (bat), word (sat), word (fat)
        
        function htmlEscape(text){
            return text.replace(/[<>"&]/g, function(match, pos, originalText){
                switch(match){
                    case "<":
                        return "&lt;";
                    case ">":
                        return "&gt;";
                    case "&":
                        return "&amp;";
                    case "\"":
                        return "&quot;";
                }             
            });
        }
        
        alert(htmlEscape("<p class=\"greeting\">Hello world!</p>")); //&lt;p class=&quot;greeting&quot;&gt;Hello world!&lt;/p&gt;

        var colorText = "red,blue,green,yellow";
        var colors1 = colorText.split(",");      //["red", "blue", "green", "yellow"]
        var colors2 = colorText.split(",", 2);   //["red", "blue"]
        var colors3 = colorText.split(/[^\,]+/); //["", ",", ",", ",", ""]


        
    </script>
</head>
<body>

</body>
</html>
  • QQ讨论群
  • 关注微信
    • 欢迎咨询
    • 欢迎加入QQ群 16506021
  • 公众号:源宝网络