• 《深入理解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);//可以实现换行
  • 相关阅读:
    echarts的legend图例的显示与隐藏(legend折线图加载完页面显示的个数)
    程序员必备网站
    web前端兼容性问题总结
    sass
    JS 中的事件绑定、事件监听、事件委托是什么?
    格式化电话号码,并不像看到的那么简单
    机器学习之Javascript篇: k-Means 聚类算法介绍
    机器学习之Javascript篇:遗传算法(2)
    机器学习之Javascript篇:遗传算法介绍
    机器学习之Javascript篇: 近邻(k-nearest-neighbor) 算法介绍
  • 原文地址:https://www.cnblogs.com/jiaxiaonuo/p/7264674.html
Copyright © 2020-2023  润新知