字符串与正则的扩展

1、字符串的扩展

  • Unicode - \u0000 ~ \uFFFF

       '\{u0061}'
       // a 
    
       '\uD842\uDfB7'
       "\u{20BB7}"
       // "?" 
    
  • 字符串遍历器 - 识别大于0xFFFF

        let text2 = String.fromCodePoint(0x20BB7);
    
        for(let i = 0;i < text2.length;i++){
            console.log(text2[i]);
        }
        // �
        // � 
    
        for (let i of text){
            console.log(i);
        }
        // "?"
    
  • 模板字符串 - 保留所有空格与换行

       const getName = () => 'Iven';
       const myName = 'Eric';
       `\`Hello\` ${getName()},I am ${myName}`
    
       //"`Hello` Iven,I am Eric"
    
  • 标签模板

       alert`111`;
       // alert(111);
    
       func`This ${ a + b} is ${ a * b}`;
       // func(['This', 'is', ''],a + b, a * b);
    
       <div>
       	<span>1111</span>
       </div>
    
  • 新增方法

    • fromCodePoint - 从Unicode码点返回对应字符
         String.fromCodePoint(0x78, 0x1f680, 0x79);
         // x?y
      
    • String.raw - 返回一个包括\在内的字符串
         String.raw`\`Hello\` I am`;
         // \`Hello\` I am
      
         `\`Hello\` I am`
         // `Hello` I am
      
         String.raw({ raw: 'test'}, 1,2,3,4);
         String.raw({ raw: ['t','e','s','t']}, 1,2,3,4);
         // t1e2s3t
      
    • codePointAt - 返回一个字符的码点(10进制)
         let s = '?a';
         s.codePointAt(0).toString(16);
         s.codePointAt(2).toString(16);
         // 61
      
    • includes - 是否包含参数字符串
         const string = 'Hello world!';
         string.includes('wo');
         // true
      
    • startsWith - 是否以某一个字符串开头
         const string = 'Hello world';
         string.includes('He');
         // true
      
    • endsWith - 是否以某一个字符串结尾
         const string = 'Hello world';
         string.includes('world');
         // true
      
    • repeat - 将原字符串重复n次
         `Hello`.repeat(2.9);
         // HelloHello
      
         `Hello`.repeat(-0.9);
         // ""
      
    • padStart - 字符串补全长度
         `hello`.padStart(10,'0123456789');
         // 01234hello
      
    • padEnd
         `hello`.padEnd(10,'0123456789');
         // hello01234
      
    • trimStart、trimEnd - 去除空格,换行,tab
         '  abc  '.trimStart();
         // abc 
      
    • matchAll - 返回正则表达式所有匹配
         for(s of 'abcabcabc'.matchAll('ab')) {
             console.log(s)
         }
      

2、正则的扩展

  • RegExp - 指定修饰符
        new RegExp(/abc/ig,'i');
        // /abc/i
    
  • 字符串正则 - 内部改为调用RegExp方法
       RegExp.prototype[Symbol.match];
       RegExp.prototype[Symbol.replace];
       RegExp.prototype[Symbol.search];
       RegExp.prototype[Symbol.split];
    
  • y修饰符 - 全局粘连匹配
       const s = 'abcdabcdabc';
       const reg1 = new RegExp('abc', 'g');
       const reg2 = new RegExp('abc', 'y');
    
       reg1.exec(s);
       reg2.exec(s);
    
  • sticky - 是否使用了y修饰符
       const reg = /hello\d/y;
       reg.sticky;
       // true 
    
  • flags - 返回正则表达式的修饰符
       /abc/ig.flags
       // gi
    
  • s - 匹配一切字符(.不能匹配回车、换行等行终止符)
       /week.month/s.test('week\nbar');
       // false
    
  • 组别名(当我们使用正则匹配时,可以把它赋给一个变量)
       // 共同定义
       const RE_DATE = /(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})/;
       const matchObj = RE_DATE.exec('1999-12-31');
    
       const year = matchObj.groups.year;
       matchObj[1];
       // 1999 
    
       const month = matchObj.groups.month;
       matchObj[2];
       // 12 
    
       const day = matchObj.groups.day;
       matchObj[3];
       // 31 
    
       let { groups: { one, two } } = /^(?<one>.*):(?<two>.*)$/u.exec('week:month');
    
  • matchAll - 返回正则表达式所有匹配 - 迭代器
       for(s of 'abcabcabc'.matchAll('ab')) {
           console.log(s)
       }
    

转载于:https://my.oschina.net/u/4144971/blog/3085009

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值