javascript怎么判断字符串以什么开头
时间:2022-02-11 16:09
判断方法:1、用“字符串对象.substr(0,比对字符个数)=="比对字符"”语句;2、用“字符串对象.slice(0,字符个数)=="比对字符"”语句;3、用“字符串对象.indexOf("比对字符")==0”语句;4、利用正则表达式。 本教程操作环境:windows7系统、javascript1.8.5版、Dell G3电脑。 javascript判断字符串以什么开头 方法1:substr()方法 方法2:slice()方法 方法3:indexOf()方法 方法4:正则 【推荐学习:javascript高级教程】 以上就是javascript怎么判断字符串以什么开头的详细内容,更多请关注gxlsystem.com其它相关文章!if("123".substr(0, 2) == "12"){
console.log(true);
}
if("123".slice(0,2) == "12"){
console.log(true);
}
if("123".indexOf("12") == 0) {
console.log(true);
}
if("123".search("12") != -1) {
console.log(true);
}if(new RegExp("^12.*$").test(12)) {
console.log(true);
}if("12".match(new RegExp("^12.*$"))) {
console.log(true);
}