ES6-字符串新增的方法

1、String.formCodePoint(Unicode码) 和 String.fromCharCode(Unicode码):将 Unicode 码转为字符
String.fromCodePoint() 可以识别大于 0xffff 的码点字符,而 String.fromCharCode() 不能识别大于 0xffff 的字符

String.fromCodePoint(0x20BB7) // '𠮷'
String.fromCharCode(0x20BB7) // 舍弃最高位,直接返回 0xBB7 对应的字符  ->  'ஷ'

String.formCodePoint()   <---字符和 码点 的互相转换-->   '字符'.codePointAt()
String.fromCharCodde()   <---字符和 码点 的互相转换-->   '字符'.charCodeAt()

2、‘字符’.codePointAt() 和 ‘字符’.charCodeAt():将字符转为十进制的码点, 如果想要十六进制的码点可以使用 toString(16)转换下
‘字符’.codePointAt()可以识别四个字节的字符,而 ‘字符’.charCodeAt() 只能识别两个字节的字符

'𠮷'.codePointAt().toString(16) // 0x20BB7
'𠮷'.charCodeAt().toString(16) // d842

3、String.raw字符: 会对字符中需要转译的字符 \ 进行再次转译

String.raw`\\ndasdasa`  // 返回 \\\\ndasdasa

4、‘Unicode字符’.normallize()方法:用来将同一字符的不同表示进行统一形式,即Unicode的正规化

'\u01D1'.normalize() === '\u004F\u030C'.normalize()  // \u01D1 和 \u004F\u030C 都表示相同的字符

5、includes()、startsWith()、endsWith():判断字符是否包含某个字符

 'asdfrew345'.includes('fre') // true --> 'asdfrew345' 是否包含 ’fre‘
 'asdfrew345'.includes('aaa') // false --> 'asdfrew345' 是否包含 ’aaa‘

 'asdfrew345'.startsWith('fre') // false --> 'asdfrew345' 的前部是否是 ’fre‘
 'asdfrew345'.startsWith('asd') // true --> 'asdfrew345' 的前部是否是 ’asd‘

 'asdfrew345'.startsWith('fre') // false --> 'asdfrew345' 的尾部是否是 ’fre‘
 'asdfrew345'.startsWith('345') // true --> 'asdfrew345' 的尾部是否是 ’345‘

6、‘字符’.repeat(n):复制原字符n次

'a'.repeat(Infinity) // RangeError  ---> 小于 0 会报错
'a'.repeat(-2) // RangeError  ---> 小于 0 会报错
'a'.repeat(-1.4) // RangeError ---> 小于 0 会报错

'a'.repeat(NaN) // ''
'a'.repeat(-0.6) // '' ---> -1~0之间 会返回空

'a'.repeat(3.2) // 'aaa' ---> 忽略小数,取整数
'a'.repeat(2) // 'aa'

7、padStart()、padEnd(): 字符串的补全

'ab'.padStart(5, 'xy') // 'xyxab'   在字符串 'ab' 的头部补全 'xy',总长度为 5
'ab'.padStart(5) // '   ab'   在字符串 'ab' 的头部默认补全空字符 '',总长度为 5
'abwew'.padStart(5, 'xy') // 'abwew'


'ab'.padEnd(5, 'xy') // 'abxyx'  在字符串 'ab' 的尾部补全 'xy',总长度为 5
'ab'.padEnd(5) // 'ab   '  在字符串 'ab' 的尾部默认补全空字符 '',总长度为 5
'abwew'.padEnd(5, 'xy') // 'abwew'

8、trim()、trimStart()、trimEnd()、trimLeft()、trimRight(): 消除字符串空格

' aa  da  '.trim() // 消除字符串的头部和尾部所有空格 --- >  'aa  da'
' aa  da  '.trimStart() // 消除字符串头部的所有空格 --- >  'aa  da  '
' aa  da  '.trimLeft() // 消除字符串头部的所有空格 --- >  'aa  da  '
' aa  da  '.trimEnd() // 消除字符串尾部的所有空格 --- >  ' aa  da'
' aa  da  '.trimRight() // 消除字符串尾部的所有空格 --- >  ' aa  da'

9、‘字符’.replace() 和 '字符’replaceAll();替换字符中的某些字符

// 替换字符串中的从左往右第一个字符 'a' 为 '-'
'aagafewrqw'.replace('a', '-') // '-agafewrqw'
// 替换字符串中的所有字符 'a' 为 '-' 
'aagafewrqw'.replace(/a/g, '-') // '--g-fewrqw'
// 替换字符串中的从左往右第一个字符(不区分大小写) 'a' 为 '-'
'Aagafewrqw'.replace(/a/i, '-') // '-agafewrqw'



// 替换字符串中的所有字符 'a' 为 '-'
'aagafewrqw'.replaceAll('a', '-') // '--g-fewrqw'
// 第二个参数可以是函数
'aabbcc'.replaceAll('b', () => '_') // 'aa__cc'
// $& 表示匹配的字符串本身,即`b`本身, 所以返回结果与原字符串一致
'abbc'.replaceAll('b', '$&')  // 'abbc'
// $` 表示匹配结果之前的字符串, 对于第一个`b`,$` 指代`a`, 对于第二个`b`,$` 指代`ab`
'abbc'.replaceAll('b', '$`') // 'aaabc'
// $' 表示匹配结果之后的字符串, 对于第一个`b`,$' 指代`bc`, 对于第二个`b`,$' 指代`c`
'abbc'.replaceAll('b', `$'`) // 'abccc'
// $1 表示正则表达式的第一个组匹配,指代`ab`,$2 表示正则表达式的第二个组匹配,指代`bc`
'abbc'.replaceAll(/(ab)(bc)/g, '$2$1') // 'bcab'
// $$ 指代 美元符号 $
'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) // 匹配的内容 ---》 123abc456
  console.log(p1) // 组1 ---》 123
  console.log(p2) // 组2 ---》 abc
  console.log(p3) // 组3 ---》 456
  console.log(offset) // 匹配内容的位置 ---》 0
  console.log(string) // 完整字符串 ---》 aa123abc456aa
  return [p1, p2, p3].join(' - ');
}
str.replaceAll(regex, replacer) // aa123 - abc - 456aa

10、‘字符’.at(索引):获取指定位置的字符

'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'

就记录到这,祝大家开心~
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值