ES(9~10) |字符串扩展

96 阅读1分钟

Offer 驾到,掘友接招!我正在参与2022春招打卡活动,点击查看活动详情

ES系列文章

ES9字符串扩展

放松了模板字符串转义序列的语法限制

const foo = (a, b, c, d) => {
    console.log(a)
    console.log(b)
    console.log(c)
    console.log(d)
}
// foo(1, 2, 3, 4)

//es6
const name = 'hhh'
const age = 18
foo`这是${name}, 他的年龄是${age}岁`

const foo = arg => {
    console.log(arg)
}
// foo`\u{61} and \u{62}`
foo`\u{61} and \unicode`

// let str = `\u{61} and \unicode`

ES10字符串扩展

const str = '   imooc    '
// 正则
console.log(str)
console.log(str.replace(/^\s+/g, '')) // 去掉前面的空格
console.log(str.replace(/\s+$/g, '')) // 去掉后面的空格

String.prototype.trimStart()

String.prototype.trimStart()方法,表示去掉字符串前面的空格或指定字符

const str = '   imooc    '
// 去掉前面的空格
console.log(str.trimStart())
console.log(str.trimLeft())

String.prototype.trimEnd()

String.prototype.trimEnd()方法,表示去掉字符串后面的空格或指定字符

const str = '   imooc    '
// 去掉后面的空格
console.log(str.trimEnd())
console.log(str.trimRight())

String.prototype.Trim()

String.prototype.Trim()方法,表示去掉字符串前面后面的空格或指定字符

const str = '   imooc    '
// 去掉前后的空格
console.log(str.trim())

一个前端小白,若文章有错误内容,欢迎大佬指点讨论!