js判断字符串是否包含某个字符
使用includes()方法:
const str = 'Hello World';
const char = 'o';
const containsChar = str.includes(char); //true
使用正则表达式:
const str = 'Hello World';
const char = 'o';
const regex = new RegExp(char);
const containsChar = regex.test(str); //true
使用indexOf()方法:
const str = 'Hello World';
const char = 'o';
const containsChar = str.indexOf(char) !== -1;//true
使用search()方法:
const str = 'Hello World';
const char = 'o';
const containsChar = str.search(char) !== -1; //true
使用match()方法:
const str = 'Hello World';
const char = 'o';
const containsChar = str.match(char) !== null;//true