标签:username strlen 五个 inpu indexof orm har char false
字符串
/* 凡是引号当中的内容都是字符串;?? */
"该号码可注册"
"12916316319"
""
"http://www.163.com"
‘http://www.163.com‘
/* str.length */
"micromajo".length //10
---
var userName = input.value;//"micromajor"
if(userName.length<6){
alert("userName must be longer than 6");
}
---
/* str.charAt(index) */
"micromajor".charAt(0) //m
---
var userName = input.value;//"-micromajor"
if(userName.charAt(0) == "-"){
alert("userName can‘t start with ‘-‘");
}
---
/* str.indexOf(searchValue[,fromIndex]) */
"micro-major".indexOf("-") //5
"micro-major-web".indexOf("-") //5
"micro-major".indexOf("major") //6
"micro-major".indexOf("-") //-1
---
var userName = input.value;//"micromajor"
if(userName.indexOf("-") == -1){
alert("userName must contain ‘-‘");
}
---
/* str.search(regexp) */
"micromajor163".search(/[0-9]/) //10
"micromajor163".search(/[A-Z]/) //-1
---
var userName = input.value;//"micromajor163"
if(userName.search(/[0-9]/) !=-1){
alert("userName can‘t contain a number");
}
---
/* str.match(regexp) */
"micormajor163".match(/[0-9]/) //["1"]
"micromajor163".match(/[0-9]/g) //["1","6","3"]
"micromajor163".match(/[A-Z]/) //null
/* str.replace(regexp|substr,newSubstr|function) */
"micromajor163".replace("163","##") //"micromajor##"
"micromajor163".replace(/[0-9]/,"#") //"micromajor#63"
"micromajor163".replace(/[0-9]/g,"#") //"micromajor##"
"micromajor163".replace(/[0-9]/g,"") //"micromajor"
/* str.substring(indexA[,indexB]) */
"micromajor".substring(5,7) //"ma"
"micromajor".substring(5) //"major"
/* str.slice(beginSlice[,endSlice]) */
"micromajor".slice(5,7) //"ma"
"micromajor".slice(5) //"major"
"micromajor".slice(1,-1) //"icromajo"
"micormajor".slice(-3) //"jor"
/* str.substr(start[,length]) */
"micromajor".substr(5,2) //"ma"
"micromajor".substr(5) //"marjor"
/* str.split([separator][,limit]) */
"micro major".split(" ") //["micro","major"]
"micro major".split(" ",1) //["micro"]
"micro2major".split(/[0-9]/) //["micro","major"]
/* str.toLowerCase() */
"MicroMajor".toLowerCase() //"micromajor"
/* str.toUpperCase() */
"MicroMajor".toUpperCase() //"MICROMAJOR"
"0571" + "-" + "88888888" //"0571-88888888"
var area = areaInput.value;//0571
var tel = telInput.value;//88888888
var number = area + "-" + tel;//0571-88888888
String(163) //"163"
String(null) //"null"
"micro\"major" //"micro"major"
"micro\\major" //"micor\major"
"micro\tmajor" //"micro major"
/* to be continued
//`${}`
let name = "zhangsan";
let occupation = "doctor";
let str = `he is ${name},he is a ${occupation}`;
//多行
let str = `write once,
run anywhere`;
//${}中可以放置任意javascript表达式
var obj = {"a":1,"b":2};
var str = `the result is ${obj.a+obj.b}`;
function fn() {
return 3;
}
var str = `the result is ${fn()}`;
标签模板,可以理解为标签函数+模板字符串
var name = "zhangsan";
var height = 1.8;
tagFn`his name is ${name},his height is ${height} meter`;
function tagFn(arr,v1,v2){
console.log(arr);//[‘his name is‘,‘his height is‘,‘meter‘]
console.log(v1);//zhangsan
console.log(v2);1.8
}
var name1 = ‘ni‘;
var name2 = name1.repeat(3);
console.log(name1)//‘ni‘
console.log(name2)//‘ninini‘
var name = "zhangsan";
name.includes(‘san‘);//true
name.includes(‘ni‘);//false
name.includes(‘z‘,1);//false 从第二个字符开始搜索
var name = "zhangsan";
name.startsWith(‘z‘);//true
name.startsWith(‘h‘);//false
name.startsWith(‘h‘,1);//true 从第二个字符开始
var name = "zhangsan";
name.endsWith(‘z‘);false
name.endsWith(‘n‘);true
name.endsWith(‘n‘,5);false 只针对前五个字符
name.endsWith(‘g‘,5);true
var str1 = "前端";
var str2 = "??";
str1.length; //length为2
str2.length; //length为2
str1.charAt(0); //前
str1.charAt(1); //端
str2.charAt(0); //‘?‘
str2.charAt(1); //‘?‘
str.codePointAt(??); //结果:134071
//这个数字抓换成16进制就是20bb7,对应的Unicode编码则是\u20bb7。
String.fromCodePoint(134071); //结果:"??"
即使字符串中含有转义符,它都视而不见,直接输出。
console.log(String.raw`hello\nwolrd`);
//输出:hello\nwolrd
标签:username strlen 五个 inpu indexof orm har char false
原文地址:https://www.cnblogs.com/luwanlin/p/14295781.html