JavaScript

ES6学习第三节:字符串的扩展

2019-01-10  本文已影响0人  ChangLau

for ... of

// 字符串遍历
const txt = 'www.coffeecola.cn'
for (var item of txt) console.log(item)

includes():返回布尔值,表示是否找到了参数字符串。
startsWith():返回布尔值,表示参数字符串是否在原字符串的头部。
endsWith():返回布尔值,表示参数字符串是否在原字符串的尾部。

// includes startsWith endsWith
console.log(txt.includes('coffeecola'))
console.log(txt.startsWith('www'))
console.log(txt.endsWith('cn'))

repeat方法返回一个新字符串,表示将原字符串重复n次。

// repeat
console.log(txt.repeat(0.1))
console.log(txt.repeat(2))
// 取整
console.log(txt.repeat(2.9))

字符串补全长度的功能

// padStart padEnd
console.log(txt.padStart(txt.length + 8, 'https://'))
console.log(txt.padEnd(txt.length + 8, '/#/login'))

Node v10.8.0不支持

var reg = /o/
console.log(txt.match(reg))
// Node不支持 v10.8.0
// console.log(txt.matchAll(reg));
var reg = /o/g
console.log(txt.match(reg))
var name = 'ChangLau'
var str = `
  \`Hello, my Name is ${name}\`
`
console.log(str)
var a = 1,
  b = 2
// 模板字符串可以进行JS运算
console.log(`${a} add ${b} equals ${a + b}`)

上一篇 下一篇

猜你喜欢

热点阅读