当前位置:网站首页>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 ~
边栏推荐
- [JS download files through url]
- Develop a simple login logic based on SSM
- 游戏思考15:全区全服和分区分服的思考
- Finally got byte offer, 25-year-old inexperienced experience in software testing, to share with you
- The author is more willing to regard industrial Internet as a concept much richer than consumer Internet
- [eight sorts ④] merge sort, sort not based on comparison (count sort, cardinal sort, bucket sort)
- How to determine whether the current script is in the node environment or the browser environment?
- Basic usage of three JS high-order functions --filter---map---reduce
- [WesternCTF2018]shrine writeup
- 970 golang realizes the communication between multithreaded server and client
猜你喜欢

Creating logical volumes and viewing and modifying attributes for AIX storage management

Learning note 3 -- Key Technologies of high-precision map (Part 1)

Liteos learning - first knowledge of development environment

XMIND mind map

ACM tutorial - quick sort (regular + tail recursion + random benchmark)

Study note 2 -- definition and value of high-precision map

Leetcode, 3 repeatless longest subsequence
![[IVX junior engineer training course 10 papers to get certificates] 01 learn about IVX and complete the New Year greeting card](/img/99/53b0ae47bada8b0d4db30d0517fe3d.jpg)
[IVX junior engineer training course 10 papers to get certificates] 01 learn about IVX and complete the New Year greeting card
![[IVX junior engineer training course 10 papers to get certificates] 0708 news page production](/img/ad/a1cb672d2913b6befd6d8779c993ec.jpg)
[IVX junior engineer training course 10 papers to get certificates] 0708 news page production

Develop a simple login logic based on SSM
随机推荐
只是以消费互联网的方式和方法来落地和实践产业互联网,并不能够带来长久的发展
首场“移动云杯”空宣会,期待与开发者一起共创算网新世界!
SAP ui5 beginner tutorial XXI - trial version of custom formatter of SAP ui5
Zak's latest "neural information transmission", with slides and videos
No converter found for return value of type: class
Bilstm CRF code implementation
The first "mobile cloud Cup" empty publicity meeting, looking forward to working with developers to create a new world of computing!
Design and control of multi rotor aircraft (VII) -- sensor calibration and measurement model
微信小程序中使用tabBar
学习笔记24--多传感器后融合技术
Global and Chinese markets for power over Ethernet (POE) solutions 2022-2028: Research Report on technology, participants, trends, market size and share
Hcip day 14 (MPLS protocol)
Two TVs
Mitsubishi PLC FX3U pulse axis jog function block (mc_jog)
[eight sorting ③] quick sorting (dynamic graph deduction Hoare method, digging method, front and back pointer method)
ACM教程 - 快速排序(常规 + 尾递归 + 随机基准数)
SQL injection for Web Security (2)
Leetcode, 3 repeatless longest subsequence
学习笔记2--高精度地图定义及价值
Minimize the error