1. 模板字符串
let str = `我可以
直接换行啦`
console.log('str: ', str);
// str: 我可以
//
// 直接换行啦
可以直接添加空格和换行
2. 带标签模板字符串
const name = 'wlq'
const gender = true
function speek(strings, name, gender) {
console.log(strings) // [ 'hey,', ' is a ', '' ]
let sex = gender ? 'man' : 'woman'
return strings[0] + name + strings[1] + sex + strings[2]
}
let res = speek`hey, ${name} is a ${gender}`
console.log('res: ', res); // res: hey, wlq is a man
第一次知道模板字符串
还可以这么搞!!!
3. 字符串扩展方法
startsWith endsWith includes
let msg = 'Error:res is false .'
console.log(msg.startsWith('Error')); // true
console.log(msg.endsWith('.')); // true
console.log(msg.includes('res')); // true
可以用来指定位置是否含有某些字符串,省的用正则了,正则就是 ****