ES6 - 字符串與正則的擴展

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)
      }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章