前端开发之JS(字符串)

格式

"zifuchuan"
""
'zifuchuan'

str.length

"micromajor".length;   //10

str.charAt(index)

"micromajor".charAt(0);  //"m"

str.indexOf(searchValue)

"micro-major".indexOf("-");  //5
"micro-ma-jor".indexOf("-");  //5
"micromajor".indexOf("-");  //-1

str.search(regexp)

"micromajor163".search(/[0-9]/);  //10 找1
"micromajor163".search(/[A-Z]/);  //-1

str.match(regexp)

"micromajor163".match(/[0-9]/);//["1"] 
"micromajor163".match(/[0-9]/g);//["1","6","3"] 
"micromajor163".match(/[A-Z]/);  //null

str.replace(regexp|substr,newSubstr)

"micromajor163".replace('163','###')
"micromajor163".replace(/[0-9]/g,'###')
"micromajor163".replace(/[0-9]/g,'')

str.substring(index[,index])截取字符串

"micromajor".substring(5,7)//"ma"
"micromajor".substring(5)//"major163"

str.slice()

"micromajor".slice(5,7) //"ma"
"micromajor".slice(5)   // "major163"
"micromajor".slice(1,-1)//"icromajo"
"micromajor".slice(-3)//"jor"

str.substr(,length)

"micromajor".substr(5,2)  //"ma"
"micromajor".substr(5)  //"major"

str.split([separator])

"micro major".split(" ")  //["micro","major"]
"micro major".split(" ",1)  //["major"]
"micro2major".split("/[0-9]/",1)  //["micro","major"]

连接

"0571"+"-"+"88888888"   //0571-88888888

String()

String(163)//"163"

转义

"micro\"major"    //"micro"major"
"micro\\major"    //"micro\major"
"micro\tmajor"    //"micro  major"

你可能感兴趣的:(前端开发之JS(字符串))