Home >Web Front-end >JS Tutorial >An example tutorial summarizing JavaScript string retrieval characters
This article mainly introduces the method of retrieving characters in JavaScript string in detail. It has a certain reference value. Interested friends can refer to
in strings. There are several ways to search for characters in for your reference. The specific contents are as follows
var text="abcdefgh你好,很高兴认识你!"; var str1="abc"; var str2="def"; var str3="ABC"; var str4="很高兴"; function isContain(str,substr){ return new RegExp(substr).test(str); } console.log(isContain(text,str1));//true console.log(isContain(text,str4));//true console.log(text.indexOf(str1));//0,如果匹配则返回其位置 console.log(text.indexOf(str2));//3 console.log(text.indexOf(str4));//11 console.log(text.indexOf(str3));//-1,如果不匹配则返回-1 console.log(text.indexOf(str1,1));//-1 第二个参数表示从下标为1的地方开始找 console.log(text.lastIndexOf(str1,1));//0,从后向前检索,返回其下标 console.log(text.lastIndexOf(str2));//3 console.log(text.substring(0,5)); //abcde 提取下标之间的字符串,包括第一个参数,不包括第二个参数 console.log(text.slice(0,5));//abcde 根substring作用基本相同 console.log(text.substr(0,3));//abc,第一个参数表示起始下标,第二个参数表示获取的字符长度 console.log(text.match(str1));//返回abc数组,可以使用正则,进行了解 console.log(text.match(str1)[0]);//abc
The above is the detailed content of An example tutorial summarizing JavaScript string retrieval characters. For more information, please follow other related articles on the PHP Chinese website!