我想将用户输入与给定字符串进行比较,例如“hello”比较“hello”应该返回true,这部分很简单,但我也希望“h”、“he”、“hel”等返回true,但不返回“洛'
您将如何使用 JavaScript 来解决这个问题?
最佳答案
一种快速简单的方法:
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
}
关于javascript - 使用 javascript 匹配完整或部分字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25219641/