I want to compare user input to a given string eg 'hello' compared 'hello' should return true that part is easy but I also want 'h', 'he', 'hel' etc to return true but not 'lo'
How would you approach this with javascript?
A quick and simple way:
var match = "hello";
var test = "hel";
if( match.substr(0,test.length) == test) {
// looking good!
// optionally, add this to the condition: && test.length > 0
// otherwise an empty test string would match
}
you need to use indexOf() function.
var hello = "hello";
if(hello.indexOf("he")===0){
//its in.
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.