03、ES6 字符串扩展

来源:互联网 发布:.net b2b商城源码 编辑:程序博客网 时间:2024/06/05 21:53

1.拼接字符串: 用反引号声明 拼接变量的方式 ${变量名}

    let num = 1;
    let str1 = `${num}`;
    console.log(str1);

1)可以执行运算
    let str2 = `${num+10}`;
    console.log(str2);

2)换行可以直接换
    let str3 = `${num+100}


    ok`;
    console.log(str3);

运行结果:


2.字符串的查找
1)includes() 如果存在 返回true 或 false

    let str = 'hello chongqing';
    console.log(str.includes('c'));
    console.log(str.includes('2'));



2)startsWith  查找是否在头部 返回true 或 false

    let str = 'hello chongqing';
    console.log(str.startsWith('h'));
    console.log(str.startsWith('2'));


3)endsWith  查找是否在尾部 返回true 或 false

    let str = 'hello chongqing';
    console.log(str.endsWith('ng'));
    console.log(str.endsWith('hl'));



4)repeat(10) 复制几遍

    let str = 'hello chongqing !';
    console.log(str.repeat(1));
    console.log(str.repeat(3));
    console.log(str.repeat(5));


5)正则表达式匹配扩展

let str = 'aaa_aa_a';
let r1 = /a+/g;
/*
 *   从上次匹配的地方开始匹配 的
 *   第一次: aaa   剩余的部分  _aa_a
 *   第二次 _aa_a 开始匹配
 * */
console.log(r1.exec(str));
console.log(r1.exec(str));
console.log('**************************************');


let r2 = /a+/y;
console.log(r2.exec(str));
console.log(r2.exec(str));















0 0
原创粉丝点击