stringObj.search(reg)
//搜索字符串中指定的子字符串
返回第一個(gè)匹配字串的起始位置(自動(dòng)忽略g/lastIndex),沒(méi)有找到匹配項(xiàng)返回-1
"haha,jack,meme".search(/jack/)
5
"haha ,jack,gaga".search(/jack/)
6
"haha ,jac,nene".search(/jack/)
-1
stringObj.match(searchStr)或stringObj.match(reg)
一種是傳入需要檢索的子串
傳入要匹配的模式的reg 如果沒(méi)有傳入g則只執(zhí)行一次搜索,傳入則可執(zhí)行多次搜索,返回所有匹配值
"haha 1 sdsa 3 sdad 4 sadsds".match(/\d+/)
["1"]
"haha 1 sdsa 3 sdad 4 sadsds".match(/\d+/g)
["1", "3", "4"]
stringObj.replace(reg|str,newStr|function[,flag])
使用一個(gè)替換值或匹配模式來(lái)替換源字符串中的某些或者所有匹配項(xiàng)
返回替換后的字符串
"hello world".replace(/world/,"jack")
"hello jack"
stringObj.split(reg|str,length) reg|str指定分隔位置,length指定返回?cái)?shù)組最大長(zhǎng)度(可選參數(shù),如不傳分隔整個(gè)字符串)
把字符串分割成字符串?dāng)?shù)組
"hello i am haha".split(/\s/)
["hello", "i", "am", "haha"]