zoukankan      html  css  js  c++  java
  • 《深入理解ES6》笔记(2)

    normalize()方法

      如果要对不同字符进行排序或者比较操作,会存在一种可能,它们是等效的。在对比字符串之前,一定先把它们标准化为同一种形式。

    let firstNormalized = first.normalize();

    正则表达式u修饰符

      当一个正则表达式添加了u修饰符时,它就从编码单元操作模式切换为字符模式。

    console.log(/^.$/u.test('lilian');

      检测u修饰符支持

    function hasRegExpU(){
        try{
            var pattern=new RegExp(".","u");
            return true;
        }catch(ex){
            return false;
        }
    }

    字符串中的子串识别

    • includes()方法
    • startsWith()方法
    • endsWith()方法

    3个方法都接受两个参数:第一个参数指定要搜索的文本,第二个参数可选,制定一个开始搜索的未知的索引值。如果指定第二个参数,则endsWith()方法从字符串长度减去这个索引值的位置开始匹配。

    let msg="hello world!";
    console.log(msg.includes('hello');//true
    console.log(msg.startsWith('h');//true
    console.log(msg.endsWith('!');//true
    console.log(msg.includes('o',5);//true
    console.log(msg.startsWith('o',5);//false
    console.log(msg.endsWith('o',6);//false

    repeat()方法

      该方法接受一个number类型的参数,表示该字符串的重复次数。

    console.log('jiaxiaonuo',repeat(2));//jiaxiaonuojiaxiaonuo

    正则表达式y修饰符

    (待加入。。。)

    正则表达式的复制

      ES5中,可以通过给RegExp构造函数传递正则表达式作为参数来复制这个整个表达式,但是如果传入第二个参数,会抛出错误。ES6修正了这个行为。

    let re1=/ab/i,
        re2=new RegExp(re1,"g");
    console.log(re1.toString());    //"/ab/i"
    console.log(re2.toString());    //"ab/g"
    
    console.log(re1.test("ab"));    //true
    console.log(re2.test("ab"));    //true
    
    console.log(re1.test("AB"));    //true
    console.log(re2.test("AB"));    //false

    flags属性

      访问flags属性会返回所有应用于当前正则表达式的修饰符字符串。

    let re=/ab/g;
    console.log(re.resource);    //"ab"
    console.log(re.flags);    //"g"

      结合source属性和flags属性可以免去格式化正则表达式之忧。

    多行字符串

      使用反撇号替换单、双引号,反撇号中的所有空白符都属于字符串的一部分

    let message=`message
    string`;
    console.log(message);//可以实现换行
  • 相关阅读:
    c语言基础学习10_文件操作02
    c语言_文件操作_FILE结构体小解释
    初识 Swift编程语言(中文版)
    Jquery滑动门实现
    【一步一步走(1)】远程桌面软件VNC的安装与配置
    并查集 路径压缩(具体解释)
    linux中操作java进程
    HDOJ 3944 DP?
    选择排序与冒泡排序
    UVa145 Gondwanaland Telecom
  • 原文地址:https://www.cnblogs.com/jiaxiaonuo/p/7264674.html
Copyright © 2011-2022 走看看