欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

记录几个常用的js api

程序员文章站 2024-02-21 16:52:34
...

记住几个js的api并不能提交自己的竞争力,但可以提高开发效率。

includes() 判断字符串中是否含有其他字符串,返回布尔类型值

const name = 'hy';
const sentence = 'Hello hy! I am lq.';
const result = sentence.includes(name);
console.log(result); // true
复制代码

startsWith() 判断某个字符串是否在调用者的开头,返回布尔类型

sentence.startsWith('He'); // true
复制代码

endsWith() 判断某个字符串是否在调用者的结尾,返回布尔类型

sentence.endsWith('lq.'); // true
复制代码

replace()方法用来替换字符串中的子串

let name = 'repeat my name: liqi liqi liqi';
let result1 = name.replace('li', 'h');
console.log(name); // 'repeat my name: liqi liqi liqi'
console.log(result1); // 'repeat my name: hqi liqi liqi';

let result2 = name.replace(/li/g, 'h');
console.log(result2); // 'repeat my name: hqi hqi hqi';复制代码