这两个方法都可以接受两个参数(i,index) i:要查找的参数,index从哪个索引开始查找
lastIndexOf()是倒过来查找,找到是最开始的位置
var str="hello world"; console.log(str.indexOf("o"));//5 console.log(str.lastIndexOf("o")); //75.字符串的截取
substr(startIndex,length);startIndex:开始的索引;length:截取总个数 var str="hello world"; console.log(str.substr(0,4));//hell console.log(str.substr(0));//hello world substring(startIndex,endIndex)\slice(startIndex,endIndex)两者唯一的区别就是slice可以接受负数而且截取的时候 不包含lastIndex值; console.log(str.substring(0,4));//hell console.log(str.substring(0));//hello world console.log(str.substring());//hello world console.log(str.slice());//hello world console.log(str.slice(0));//hello world console.log(str.slice(0,4));//hello world6.大写小转换(toLowerCase()/toUpperCase())
7.replace(newStr,oldStr)替换 old替换new(后者替换前者)
var old="HELLO WORLD"; //console.log(str.replace(str,old)) //console.log(str.replace(/o/g,"a"));//返回的是字符串;8.split(拆分符号) 将字符串转换为数组
var str="hello world" console.log(str.split(""));//["h", "e", "l", "l", "o", " ", "w", "o", "r", "l", "d"]9.match() ;比较特殊
var str2="a1b2c3" console.log(str2.match("1"));// ["1", index: 0, input: "1a2b3c4d5e"] console.log(str2.match(/\d+/g));// ["1", "2", "3", "4", "5"] var str = '?keyword=1&value=haha'; var reg = new RegExp("(^|&)" + "keyword" + "=([^&]*)(&|$)"); var result =str.substr(1);//keyword=1&value=haha console.log(result.match(reg));//["keyword=1&", "", "1", "&", index: 0, input: "keyword=1&value=haha"]