一、substr() 方法
if("123".substr(0, 2) == "12"){
console.log(true);
}
二、substring() 方法
if("123".substring(0, 2) == "12"){
console.log(true);
}
三、slice()方法
if("123".slice(0,2) == "12"){
console.log(true);
}
四、indexOf() 方法
if("123".indexOf("12") == 0) {
console.log(true);
}
五、startsWith(),endsWith()
console.log("123".startsWith("12"));
console.log("123".endsWith("23"));
if (typeof String.prototype.startsWith != 'function') {
String.prototype.startsWith = function (prefix){
return this.slice(0, prefix.length) === prefix;
};
}
六、正则
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);
}