Day09 Js continue

作者: 小章鱼Milo | 来源:发表于2018-07-02 18:12 被阅读0次

    1. 数组

    • 1.1 数组的构造

    <script >
      //1.直接給值
      let arr=[1,2,3]
      //2.构造函数的方式
      let b=new Array();
      b[0]=1;
      console.log(b[0])
      //检测数组
      //1.用typeof 看数组 是对象
      console.log(typeof b)
      //2.用instancof Array 来看 返回值为TRUE
      console.log(b instanceof Array);
      //3.Array.isArray 返回true
      console.log(Array.isArray(b))
    
    </script>
    
    • 1.2数组的方法

    <script>
      //数组的添加
    
      //改变数组原来的内容
      //push 从后增加
      let arr=[1,2,3]
      arr.push(4);
      console.log(arr)
      //unshift 从头(前)添加
      arr.unshift(0);
      //concat() 不改变原数组的内容,返回一个拼接后的数组
      let b=arr.concat(5);
      console.log(b)
      console.log(arr);
    
      //数组的克隆
      //1.遍历push
      let c=[]
      for (let i=0;i<arr.length;i++){
        c.push(arr[i])
      }
      console.log(c)
      //2.使用concat添加 空数组[].concat
      let d=[4,5,6]
      let e=[].concat(d)
      console.log(e)
    
      //数组的删除
      //1.从后删除 pop();
      let f=[1,2,3]
      f.pop()
      console.log(f)
      //2.从头删除
      //shift
      f.shift()
      console.log(f)
    
      //数组的修改
      //splice(index,num,item) 先删除再插入 改变原来的数组
      //index  数组的下标
      //num 删除个数
      //item 插入的参数
    
      let g=[1,2,3,4]
      g.splice(1,1,"h")
      console.log(g)
      //splice 补充
      // 增加
      let j=[1,2,3,4];
      j.splice(3,0,"hello")
      console.log(j)
      //删除
     let k=[1,2,3,4];
      k.splice(0,2)
      console.log(k)
    
      //数组的查询
      let h=[1,2,3,4]
      //1.通过下标
      h[1];
      //2.查值对应的下标
      console.log(h.indexOf(4))
      //3.slice(startIndex,endIndex) 不包含endIndex
      console.log(h.slice(2,3))
      //slice(startIndex) 从startIndex一直到最后
      console.log(h.slice(1))
      //join(separator分隔符) 方法 将数组转化成一个字符串 默认为“,”
      let l=["hello","world","good"];
      let m=l.join("|");
      console.log(m)
    
      //数组的排序
      let aa=[22,2,12,33,14];
      // 数组排序 没用
      //升序
      let bb=aa.sort(function (a,b) {
        return a-b;
      })
      // 降序
      // let cc=aa.sort(function (a,b) {
      //   return b-a;
      // })
      console.log(bb)
      // console.log(cc)
    
      //求和
      let dd=[1,2,3,4,5]
      let sum=dd.reduce(function (a,b) {
        return a+b
      })
      console.log(sum)
    
      //reverse 倒置 颠倒数组中的元素
      let ee=[1,2,3,4]
      let ff=ee.reverse();
      console.log(ff)
    
    </script>
    

    2.String方法

    <script >
      let str="hello world"
      //获取字符串的长度
      let len=str.length
      console.log(len)
      //字符串增加
      //concat 原来的字符串不变  返回新的拼接后的字符串
    
      let add=str.concat("add")
      console.log(str)
      console.log(add)
      //字符串 查询
      //charAt 输出对应下标的字符
      let char=str.charAt(1)
      console.log(char)
      //indexOf 输出对应字符的下标 如果没有返回-1
      let index=str.indexOf("l")
      console.log(index)
      //字符串的截取
      //slice()
      let a="hello world"
      let b=a.slice(1,4)
      console.log(b)
      //substr(startIndex,length)
      let subStr=a.substr(1,4)
      console.log(subStr)
      //substring(startIndex,endIndex)
      let subString=a.substring(1,4)
      console.log(subString)
      //分割
      let c=a.split("l")
      console.log(c)
      let d=a.split("")
      console.log(d)
      //search方法 返回搜索字符的下标 不存在返回-1
      let e=a.search("l")
      console.log(e)
      //match 方法 将匹配的内容返回一个数组 不存在返回Null
      let f =a.match("l")
      console.log(f)
      //replace 方法 只替换第一个查到的值
      let g=a.replace("l","h")
      console.log(g)
    </script>
    

    3.正则表达式

    • 3.1 正则表达式的构造

    <script >
      //string中match,search,split,replace支持正则表达式
      //将所有l转换成*
      // /l/g 两个斜杠里面的内容 g表示全局搜索
      let a="hello world";
      let reg=/l/g;
      let b=a.replace(reg, "*")
      console.log(b)
      //构造函数的方式创建
      // 参数1:pattern
      //参数2:attributes
      let rege=new RegExp("l","g");
    
    </script>
    
    • 3.2备选字符集

    <script >
      //备选字符集[海天]
      //[] 表示括号内任意一个字符匹配
      let a="上海,上天,上去,上海那";
      let reg=/上[海天]/g
      let b=a.replace(reg, "*")
      console.log(b)
      let c="12535_3dd"
      // let d=/[\d]/g
      let d=/[0-9]/g
      console.log(c.replace(d, "*"))
      //连续的备选字符
      //[a-z] 所有的小写字母
      //[A-Z] 所有的大写字母
      //[0-9] 所有的数字 \d
      //[0-9a-zA-Z] 所有数字和字母
      // \w 任意一个数字,字母或者下划线
      // \s 一位空字符 空格 tab 换行
      // . 处换行外其余所有字符
      let e=/[0-9a-zA-Z]/g
      console.log(c.replace(e, "*"))
    </script>
    
    • 3.3 量词

    <body>
    <input type="text" id="txt">
    <button id="btn">确定</button>
    <script >
      //量词
      //确定的数量
      //{6}  出现6次
      //{3,6} 出现3-6次
      //{3,} 至少出现3次
      //不确定的数量
      //? 出现0次或者1次
      //+ 至少出现1次
      //* 出现多次或者不出现
      //test() 方法
      //regExp.test(str) 返回boolean
      let txt=document.getElementById("txt")
      let btn=document.getElementById("btn")
      let reg=/^\d+$/
    
      // btn.onclick=function () {
      //   let value=txt.value
      //   let result=reg.test(value)
      //   alert(result)
      // }
      //或 和分组
      //|表示或
      //()表示分组
    
      //判断手机号
      // 以+86或0086开头 可有可无
      // 可以有多个空格或者没有
      //号码第一位为1 第二位为3456789中一个 最后9位为数字
    
      let rr=/1[3456789]\d{9}$/
      let rreg=/^(\+86|0086)?\s*1[3456789]\d{9}$/
    btn.onclick=function () {
        let value=txt.value;
        let result=rr.test(value)
      alert(result)
    }
      //转义字符  " 用 \"来转义
      //指定位置
      //^正则表达式 以该正则表达式开头
      //正则表达式$ 以该正则表达式结尾
    
      //将开始位置的空格清空
      let str="   hello  hello"
      let rrr=/^\s+/g
      console.log(str.replace(rrr, ""))
    
      //排除 [^abc] 除了abc以外的值
      let strr="abc232"
      let regr=/[^abc]/g
      console.log(strr.replace(regr, "*"))
    </script>
    </body>
    
    • 3.4过滤

    <body>
    <textarea name="" id="txt" cols="30" rows="10"></textarea>
    <button id="btn">过滤</button>
    <script >
      // let reg=/天猫|淘宝/g
      let reg=/<[^<>]*>/g
      let txt=document.getElementById("txt")
      let btn=document.getElementById("btn")
      btn.onclick=function () {
        let value=txt.value;
        let filter=value.replace(reg, "")
        txt.value=filter;
    
      }
    </script>
    </body>
    

    相关文章

      网友评论

        本文标题:Day09 Js continue

        本文链接:https://www.haomeiwen.com/subject/vxgluftx.html