zoukankan      html  css  js  c++  java
  • js正则和数组

     

    什么是 RegExp?

    RegExp 是正则表达式的缩写。

    RegExp 对象有 3 个方法:test()、exec() 以及 compile()

    test() 方法检索字符串中的指定值。返回值是 true 或 false。

    exec() 方法检索字符串中的指定值。返回值是被找到的值。如果没有发现匹配,则返回 null。

    compile()

    compile() 方法用于改变 RegExp。

    compile() 既可以改变检索模式,也可以添加或删除第二个参数。

    正则的对象分为那几种?

     

     

     

     正则例子:

    var patt = new RegExp("javascript");
    var res = patt.test('this is javascript course');                                      //检查有没有js
    console.log(res);                                                        //有
    patt = /javascript/;
    patt = /JavaScript/i;                                                  //对大小写不敏感
    res = patt.test("this is javascript show time");
    console.log(res);

    test方法
    //[] 方括号
    res = /[abc]/.test('la');                                                       //错
    // res = /[^abc]/.test('lue');                                                      //对
    // res = /[0-9]/.test('this is a test');                                                 //错
    // res = /[a-z]/.test('234235453245');                                                //错
    // res = /php|javascript|ios/i.test('PHP');                                               //或逻辑 对
    console.log(res);

    //元字符
    res = /./.test(' ');                                                      ///n/r省略换行符和行结束符 其余为真
    res = /./.test('this is a test');                                                      //真
    res = /w/.test('hello nana ')                                                 // 什么不是单词字符[a-zA-Z0-9]
    res = /w/.test('!#@w');
    res = /W/.test('!#%9');                                                       //有9执行小写的
    console.log(res);                                                        //[^a-zA-Z0-9] 取相反
    res = /s/.test('hello world');                                                    //空白字符
    res = /S/.test('');                                                          //非空白
    // res = /go/.test('good');                                                      //开头和结尾边界
    // res = /o/.test('good');
    // res = /d/.test('good');
    // res = /oB/.test('good');                                                    //g也是正确的 //非单词边界
    console.log(res);
    //量词
    res = /o+/.test('google');                                                       //>=1
    res = /o*/.test('google');                                                        //>=0
    res = /o?/.test('google');                                                       //>1=0
    res = /o{2}/.test('goooogle');                                                      //true
    res = /o{1,3}/.test('goooogle');                                                     //1到多少
    res = /^k/.test('king');                                                          //k开头的
    res = /i$/.test('mai');                                                          //i结尾的
    res = /o(?=w)/.test('helloworld');                                                      //后面紧给的 全局
    res = /o(?!w)/.test('helloworld');                                                    //后面不是w的 全局
    res = /d/.test('aajkldsfj');                                                       //[0-9] //false
    res = /D/.test('sdfkjllsdfj');                                                       //[^0-9] //非数字的
    console.log(res);

    //exec方法

    除了数组元素和 length 属性之外,exec() 方法还返回两个属性。index 属性声明的是匹配文本的第一个字符的位置。input 属性则存放的是被检索的字符串 string。

    Array(1)0: "is"groups: undefinedindex: 2input: "this is a test"length: 1__proto__: Array(0)


    res = /is/i.exec('this is a test');
    console.log(res);
    console.log(res[0]);

    当 RegExpObject 是一个全局正则表达式时,exec() 的行为就稍微复杂一些。它会在 RegExpObject 的 lastIndex 属性指定的字符处开始检索字符串 string。找到i从i的最后一个字符后面开始查找                                                     //is
    var str = 'this is a test hello nana hello world';
    var patt = /i/ig;
    var myArr;
    while((myArr = patt.exec(str)) !== null) {
    var msg = '找到了' + myArr[0] + '!';
    msg += '下一个匹配从' + patt.lastIndex;                                     //找到i从i的最后一个字符后面开始查找
    console.log(msg);
    }


    var str = 'this is a test';
    res = str.match(/IS/i);                                                     //只查询一次
    console.log(res);


    res = str.match(/IS/ig);                                                   //两个数组
    console.log(res);


    res = str.search(/is/i);                                                  //从几开始
    console.log(res);


    var str1 = str.replace(/is/ig, '!');                                             //替换成感叹号
    console.log(str1);


    var str = '2015-09-27';
    res = str.replace(/(d{4})-(d{2})-(d{2})/, '$2/$3/$1');
    console.log(res);


    str = 'Hello nana Edu';
    res = str.replace(/[A-Z]/g, func);                                            //输出结果变为kinghello
    function func(match) {
    return 'king_' + match.toLowerCase();
    }
    console.log(res);


    res = str.split("");                                                    //分割
    console.log(res);

    什么是数组?


    数组对象的作用是:使用单独的变量名来存储一系列的值。

    //数组直接量(字面量)形式创建数组
    var arr=[];                                                  //空数组
    var arr1=[1,2,3,4,5,6];
    var arr2=[1,2.3,true,false,null,undefined,[1,2,3],{x:1,y:2}];                                                                                       //可以放很多数据  包括对象和数组
    var x=1;
    var arr3=[x,x*3,x+2,x+3];                                                            //输出结果为1.3.1.4
    console.log(arr3);
    var arr4=[1,,3];                                          //[1,undefined,3]
    console.log(arr4[1]);                                          //undenfiend
    console.log(arr4.length);                                         //3
    var arr5=[,,];                                             //逗号后面是算长度的
    console.log(arr5.length);                                         //2
    //通过构造函数Array()创建数组
    var a=new Array();//[]
    var a=new Array(5);                                                                 //控制长度
    console.log(a.length);
    var a=new Array(1,2,'king',false,2.3,null);                                  //[1,2,'king',false,2.3,null]返回原来的
    console.log(a);

    数组长度值为4,但查看为0开始
    var arr=[1,2,3,4];
    console.log(arr[0]);                                              //1
    console.log(arr[3]); 

                                                 //4
    arr[0]='king';                                                //1变为King
    console.log(arr);


    arr[5]='hello nana';                                                     //添加hellonana
    console.log(arr);


    var x=2;                                                   //king 2 3 4 hello nn
    console.log(arr[x]);                                                   //结果为3
    arr[arr[x]]=333;                                                 //king 2 3 333 hello nn  //把4换为3333
    console.log(arr);


    var obj={x:"2",y:"4"};
    obj[0]='a';
    obj[1]='b';
    console.log(obj);
    console.log(obj.length);                                              //对象没有length属性

    var arr=[];
    arr[0]='a';
    arr[1]='b';                                                    //只能读取到2个
    arr[-123]='c';
    arr[2.3]='d';
    arr[null]='e';
    arr[false]='f';
    arr[undefined]='g';
    arr['name']='h';
    arr[4]="pp";
    console.log(arr);                                                     //长度为5
    //console.log(arr.length);


    var arr=new Array(10);
    arr=[];                                                            //覆盖为空 为0
    arr[100]='abc';
    console.log(arr.length);                                                     //101长度

    var arr=[1,2,3,4,5,6,7,8];
    arr.length=3;
    console.log(arr);                                                       //1.2.3

    arr.length=0;
    console.log(arr);                                                       //0

    arr.length=5;
    console.log(arr);                                                    //empty*5

    var arr=[1,2,3];
    Object.defineProperty(arr,'length',{                                                 //不可改变输出原来的
    writable:false
    });

    console.log(arr.length);


    arr.length=10;
    console.log(arr.length);                                                  //数组还是3

    如何删除数组开头结尾,添加等

    var arr=[];
    arr[0]='a';
    arr[1]='b';
    arr.shift();                                                        //开头删除
    arr.push('d','e','f',12,34);                                                    //尾部添加
    console.log(arr);


    var res=arr.pop();                                                       //尾部删除
    console.log(res);
    // console.log(arr);
    arr.unshift(1,2,3,4,5);                                                     //开头添加
    console.log(arr);



    res=arr.shift();
    console.log(res);
    console.log(arr);

    var arr=['a','b','c','d'];
    delete arr[0];                                                        //删除后位置保留  长度还是为4
    console.log(arr);
    console.log(arr.length);

  • 相关阅读:
    Ubuntu中遇到Unable to lock the administration directory
    Ubuntu使用脚本安装Docker
    Linux shell 批量运行jmeter脚本
    clip原理
    模块化定义JS,让统一文件夹内相同的变量不冲突
    JS中的this都有什么作用?
    jQ中prop与attr的区别
    ;(function( $, window, undefined ){ }(jQuery,window))为何需要往里面传$,window,undefined这些参数
    Asp.Mvc中的text实现 辅助用户输入 灰色字体
    C#List的排序和简单去重总结
  • 原文地址:https://www.cnblogs.com/lh0127/p/9617213.html
Copyright © 2011-2022 走看看