const name = "lin";
const str = `my name is ${name}`;
console.log(str);
标签函数 对模板字符串进行加工
const name = "lin";
const gender = true;
function myTagF(string, name, gender){
// const sex = gender ? 'man' : 'woman';
// return string[0] + name + string[1] + sex + string[2];
return string[0] + name + string[1] + gender + string[2];
}
//将模板字符串解析返回一个数组['hey,', ' is a ', '.'];
const result = myTagF`hey,${name} is a ${gender}.`;
//返回 hey,lin is a true.
console.log(result);
5.字符串扩展方法
includes()
判断字符串是否包含某个值,方便字符串查找
startsWith()
判断字符串开始是否包含某个值
endsWith()
判断字符串结尾是否包含某个值
let msg = 'this is a message';
console.log(msg.includes('o')); // false
console.log(msg.startsWith('t')); // true
console.log(msg.endsWith('t')); // false