欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

JavaScript字符串檢索字符的方法

 更新時間:2017年06月23日 16:21:45   作者:小太陽zxr  
這篇文章主要為大家詳細介紹了JavaScript字符串檢索字符的方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下

在字符串中檢索字符的幾種方式,供大家參考,具體內(nèi)容如下

var text="abcdefgh你好,很高興認(rèn)識你!";
 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 第二個參數(shù)表示從下標(biāo)為1的地方開始找

 console.log(text.lastIndexOf(str1,1));//0,從后向前檢索,返回其下標(biāo)
 console.log(text.lastIndexOf(str2));//3

 console.log(text.substring(0,5));
 //abcde 提取下標(biāo)之間的字符串,包括第一個參數(shù),不包括第二個參數(shù)

 console.log(text.slice(0,5));//abcde 根substring作用基本相同

 console.log(text.substr(0,3));//abc,第一個參數(shù)表示起始下標(biāo),第二個參數(shù)表示獲取的字符長度


 console.log(text.match(str1));//返回abc數(shù)組,可以使用正則,進行了解
 console.log(text.match(str1)[0]);//abc

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論