当前位置:网站首页>ES6 new method of string
ES6 new method of string
2022-07-02 01:19:00 【I'm Lao Wang】
1、String.formCodePoint(Unicode code ) and String.fromCharCode(Unicode code ): take Unicode Code to character
String.fromCodePoint() Can identify greater than 0xffff Code point character , and String.fromCharCode() Cannot identify greater than 0xffff The characters of
String.fromCodePoint(0x20BB7) // '𠮷'
String.fromCharCode(0x20BB7) // Abandon the highest position , Go straight back to 0xBB7 Corresponding characters -> 'ஷ'
String.formCodePoint() <--- Characters and Code points Cross conversion --> ' character '.codePointAt()
String.fromCharCodde() <--- Characters and Code points Cross conversion --> ' character '.charCodeAt()
2、‘ character ’.codePointAt() and ‘ character ’.charCodeAt(): Convert characters to decimal code points , If you want hexadecimal code points, you can use toString(16) Switch down
‘ character ’.codePointAt() Four byte characters can be recognized , and ‘ character ’.charCodeAt() Only two byte characters can be recognized
'𠮷'.codePointAt().toString(16) // 0x20BB7
'𠮷'.charCodeAt().toString(16) // d842
3、String.raw character
: Will translate the characters that need to be translated \ Translate again
String.raw`\\ndasdasa` // return \\\\ndasdasa
4、‘Unicode character ’.normallize() Method : It is used to unify different representations of the same character , namely Unicode Normalization of
'\u01D1'.normalize() === '\u004F\u030C'.normalize() // \u01D1 and \u004F\u030C Both represent the same characters
5、includes()、startsWith()、endsWith(): Determine whether a character contains a character
'asdfrew345'.includes('fre') // true --> 'asdfrew345' Does it include ’fre‘
'asdfrew345'.includes('aaa') // false --> 'asdfrew345' Does it include ’aaa‘
'asdfrew345'.startsWith('fre') // false --> 'asdfrew345' Is the front of ’fre‘
'asdfrew345'.startsWith('asd') // true --> 'asdfrew345' Is the front of ’asd‘
'asdfrew345'.startsWith('fre') // false --> 'asdfrew345' Whether the tail of is ’fre‘
'asdfrew345'.startsWith('345') // true --> 'asdfrew345' Whether the tail of is ’345‘
6、‘ character ’.repeat(n): Copy the original character n Time
'a'.repeat(Infinity) // RangeError ---> Less than 0 Will report a mistake
'a'.repeat(-2) // RangeError ---> Less than 0 Will report a mistake
'a'.repeat(-1.4) // RangeError ---> Less than 0 Will report a mistake
'a'.repeat(NaN) // ''
'a'.repeat(-0.6) // '' ---> -1~0 Between Will return to empty
'a'.repeat(3.2) // 'aaa' ---> Ignore decimals , Integer
'a'.repeat(2) // 'aa'
7、padStart()、padEnd(): Completion of string
'ab'.padStart(5, 'xy') // 'xyxab' In string 'ab' Head complement 'xy', The total length of 5
'ab'.padStart(5) // ' ab' In string 'ab' The header of the default fill empty characters '', The total length of 5
'abwew'.padStart(5, 'xy') // 'abwew'
'ab'.padEnd(5, 'xy') // 'abxyx' In string 'ab' Tail completion of 'xy', The total length of 5
'ab'.padEnd(5) // 'ab ' In string 'ab' The default completion of the tail of empty characters '', The total length of 5
'abwew'.padEnd(5, 'xy') // 'abwew'
8、trim()、trimStart()、trimEnd()、trimLeft()、trimRight(): Eliminate string spaces
' aa da '.trim() // Eliminate all spaces at the beginning and end of the string --- > 'aa da'
' aa da '.trimStart() // Eliminate all spaces in the string header --- > 'aa da '
' aa da '.trimLeft() // Eliminate all spaces in the string header --- > 'aa da '
' aa da '.trimEnd() // Eliminate all spaces at the end of the string --- > ' aa da'
' aa da '.trimRight() // Eliminate all spaces at the end of the string --- > ' aa da'
9、‘ character ’.replace() and ' character ’replaceAll(); Replace some characters in the characters
// Replace the first character from left to right in the string 'a' by '-'
'aagafewrqw'.replace('a', '-') // '-agafewrqw'
// Replace all characters in the string 'a' by '-'
'aagafewrqw'.replace(/a/g, '-') // '--g-fewrqw'
// Replace the first character from left to right in the string ( Case insensitive ) 'a' by '-'
'Aagafewrqw'.replace(/a/i, '-') // '-agafewrqw'
// Replace all characters in the string 'a' by '-'
'aagafewrqw'.replaceAll('a', '-') // '--g-fewrqw'
// The second argument can be a function
'aabbcc'.replaceAll('b', () => '_') // 'aa__cc'
// $& Represents the matching string itself , namely `b` In itself , So the returned result is consistent with the original string
'abbc'.replaceAll('b', '$&') // 'abbc'
// $` Represents the string before the matching result , For the first `b`,$` Refer to `a`, For the second `b`,$` Refer to `ab`
'abbc'.replaceAll('b', '$`') // 'aaabc'
// $' Represents the string after the matching result , For the first `b`,$' Refer to `bc`, For the second `b`,$' Refer to `c`
'abbc'.replaceAll('b', `$'`) // 'abccc'
// $1 Represents the first group match of a regular expression , Refer to `ab`,$2 Represents the second group match of regular expressions , Refer to `bc`
'abbc'.replaceAll(/(ab)(bc)/g, '$2$1') // 'bcab'
// $$ Refer to Dollar symbol $
'abc'.replaceAll('b', '$$') // 'a$c'
const str = 'aa123abc456aa', regex = /(\d+)([a-z]+)(\d+)/g;
function replacer(match, p1, p2, p3, offset, string) {
console.log(match) // Matching content ---》 123abc456
console.log(p1) // Group 1 ---》 123
console.log(p2) // Group 2 ---》 abc
console.log(p3) // Group 3 ---》 456
console.log(offset) // The location of the matching content ---》 0
console.log(string) // Full string ---》 aa123abc456aa
return [p1, p2, p3].join(' - ');
}
str.replaceAll(regex, replacer) // aa123 - abc - 456aa
10、‘ character ’.at( Indexes ): Gets the character at the specified position
'das'.at(-1) // 's'
'das'.at(-2) // 'a'
'das'.at(-3) // 'd'
'das'.at(-4) // undefined
'das'.at(666) // undefined
'das'.at(2) // 's'
'das'.at(1) // 'a'
'das'.at(0) // 'd'
'das'.at(true) // 'a'
'das'.at(false) // 'd'
'das'.at(NaN) // 'd'
'das'.at('fafdafdafdsafa') // 'd'
That's the record , I wish you all happiness ~
边栏推荐
- Global and Chinese market of aircraft MRO software 2022-2028: Research Report on technology, participants, trends, market size and share
- You probably haven't noticed the very important testing strategy in your work
- 电商系统中常见的9大坑,你踩过没?
- ACM教程 - 快速排序(常规 + 尾递归 + 随机基准数)
- [IVX junior engineer training course 10 papers to get certificates] 01 learn about IVX and complete the New Year greeting card
- PLC Analog input analog conversion FB s_ ITR (Mitsubishi FX3U)
- [Chongqing Guangdong education] Tianshui Normal University universe exploration reference
- ACM tutorial - quick sort (regular + tail recursion + random benchmark)
- Single chip microcomputer -- hlk-w801 transplant NES simulator (III)
- Since I understand the idea of dynamic planning, I have opened the door to a new world
猜你喜欢
Excel PivotTable
We should make clear the branch prediction
Datawhale community blackboard newspaper (issue 1)
[eight sorts ①] insert sort (direct insert sort, Hill sort)
Edge computing accelerates live video scenes: clearer, smoother, and more real-time
How does schedulerx help users solve the problem of distributed task scheduling?
6-3 vulnerability exploitation SSH environment construction
Sql--- related transactions
Entrepreneurship is a little risky. Read the data and do a business analysis
Recommend an online interface mock tool usemock
随机推荐
Global and Chinese market of aircraft MRO software 2022-2028: Research Report on technology, participants, trends, market size and share
Evolution of Himalayan self-developed gateway architecture
Error creating bean with name ‘stringRedisTemplate‘ defined in class path re
Docker安装Oracle_11g
How does schedulerx help users solve the problem of distributed task scheduling?
XMIND mind map
Brief description of grafana of # yyds dry goods inventory # Prometheus
[IVX junior engineer training course 10 papers to get certificates] 01 learn about IVX and complete the New Year greeting card
uTools
Excel search and reference function
Tencent cloud techo youth dream campus trip into Wuhan University
Cookie, session, tooken
Luogu p1775 stone merger (weakened version)
The pain of Xiao Sha
Global and Chinese markets for power over Ethernet (POE) solutions 2022-2028: Research Report on technology, participants, trends, market size and share
No converter found for return value of type: class
关于ASP.NET CORE使用DateTime日期类型参数的一个小细节
Day 13 of hcip (relevant contents of BGP agreement)
How to determine whether the current script is in the node environment or the browser environment?
GL Studio 5 installation and experience