关闭

javascript 之 RegExp Function String

标签: javascript
1593人阅读 评论(0) 收藏 举报
分类:

ECMAScript通过RegExp类型来支持正则表达式

    <script type="text/javascript">
        var pattern1 = /\[bc\]at/i;
        
        alert(pattern1.global);     //false
        alert(pattern1.ignoreCase); //true
        alert(pattern1.multiline);  //false
        alert(pattern1.lastIndex);  //0
        alert(pattern1.source);     //"\[bc\]at"

        var pattern2 = new RegExp("\\[bc\\]at", "i");
        
        alert(pattern2.global);     //false
        alert(pattern2.ignoreCase); //true
        alert(pattern2.multiline);  //false
        alert(pattern2.lastIndex);  //0
        alert(pattern2.source);     //"\[bc\]at"

    </script>

Function类型

1.没有函数重载

2.解析器会先读函数声明,使其在执行任何代码之前可用,函数表达式必须要等到解析器执行到所在行才可以

    <script type="text/javascript">
        alert(sum(10,10));    //20
    
        function sum(num1, num2){
            return num1 + num2;
        }        

    </script>

3.函数也可以作为值返回

    <script type="text/javascript">
        function callSomeFunction(someFunction, someArgument){
            return someFunction(someArgument);
        }

        function add10(num){
            return num + 10;
        }
        
        var result1 = callSomeFunction(add10, 10);
        alert(result1);   //20
        
        function getGreeting(name){
            return "Hello, " + name;
        }
        
        var result2 = callSomeFunction(getGreeting, "Nicholas");
        alert(result2);   //Hello, Nicholas

    </script>

String 类型

    <script type="text/javascript">
    
        var stringObject = new String("hello world");
        var stringValue = "hello world";
        
        alert(typeof stringObject);   //"object"
        alert(typeof stringValue);    //"string"
        alert(stringObject instanceof String);  //true
        alert(stringValue instanceof String);   //false
        
    </script>

URI编码方法

    <script type="text/javascript">
    
        var uri = "http%3A%2F%2Fwww.wrox.com%2Fillegal%20value.htm%23start";
        
        //http%3A%2F%2Fwww.wrox.com%2Fillegal value.htm%23start
        alert(decodeURI(uri));
        
        //http://www.wrox.com/illegal value.htm#start
        alert(decodeURIComponent(uri));
        
    </script>






0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:381634次
    • 积分:5759
    • 等级:
    • 排名:第4473名
    • 原创:200篇
    • 转载:0篇
    • 译文:0篇
    • 评论:70条
    博客专栏
    博客已搬至