<script>
let name = "zyqqwe"
console.log(name.length);
console.log(name.toUpperCase());
console.log(name.toLowerCase());
let site = " 23123 ";
console.log(site.length);
console.log(site.trim().length);//trim就是消除空格
</script>
字符串截取
<script>
let hd = "zyqqwqweqwee"
//slice substr substring
console.log(hd.slice(1))
console.log(hd.substring(1))
console.log(hd.substr(1));
</script>
当只有一个参数的时候,都表示从第几个后开始截取,三者相差不大。
console.log(hd.slice(1,3));
console.log(hd.substring(1,3))
console.log(hd.substr(1,3));
当slice,substring加上第二个参数,就表示从第几个后开始截取到第几个。
而substr的第二个参数则表示,从第几个后开始截取几个
<script>
let hd = "zyqqwqweqwee"
//slice substr substrin
console.log(hd.slice(-3));
console.log(hd.substring(-3))
console.log(hd.substr(-3));
</script>
当参数变成负数时。slice和substr从后面开始算,而substring相当于从0开始
检索字符串
const hd = "zyqhstsl";
console.log(hd.includes("s",1));
第一个参数为要检测值(可以是字符串),第二个表示从第几个开始,负数也行,返回布尔值。
<script>
const word= ["javascript","tainanle"];
const string = "javascript太难了(tainanle)";
const status = word.some(word=>{
console.log(word);
return string.includes(word);
})
if(status){
console.log("找到了关键词");
}
</script>
some函数有点像短路运算。找到了一个为真,就会返回。
修改word的第一个词。
some函数就会继续查找。
reduce
<script>
const word = ["php","css"];
const string = "我喜欢学习php与css知识";
const replaceString =word.reduce((pre,word)=>{//会将string传给pre变量
return pre.replace(word,`<a href="#">${word}</a>`);
},string)
document.body.innerHTML+=replaceString;
</script>
reduce函数会循环数组里的个数次,搜索所有符合word数组。