wcc526的专栏

博客已搬至http://www.wengchichi.com

javascript 之 RegExp Function String

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>






阅读更多
文章标签: javascript
个人分类: javascript
上一篇github 之 如何在项目中加入coverage
下一篇javascript 之 Math JSON
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭