Unicode - \u0000 ~ \uFFFF正則表達式
'\{u0061}' // a '\uD842\uDfB7' "\u{20BB7}" // "𠮷"
字符串遍歷器 - 識別大於0xFFFFspa
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); } // "𠮷"
模板字符串 - 保留全部空格與換行prototype
const getName = () => 'Iven'; const myName = 'Eric'; `\`Hello\` ${getName()},I am ${myName}` //"`Hello` Iven,I am Eric"
標籤模板code
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) }
new RegExp(/abc/ig,'i'); // /abc/i
RegExp.prototype[Symbol.match]; RegExp.prototype[Symbol.replace]; RegExp.prototype[Symbol.search]; RegExp.prototype[Symbol.split];
const s = 'abcdabcdabc'; const reg1 = new RegExp('abc', 'g'); const reg2 = new RegExp('abc', 'y'); reg1.exec(s); reg2.exec(s);
const reg = /hello\d/y; reg.sticky; // true
/abc/ig.flags // gi
/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');
for(s of 'abcabcabc'.matchAll('ab')) { console.log(s) }