可以使用正则表达式来比较和匹配一组单词吗?
Can a regular expressions be used to compare and match set of words?
可以使用正则表达式来比较和匹配一组单词吗?
比如我想要这样的字符串...
"loan nation"
匹配这些...
"Loan Origination"
"international loans"
but not on these...
"home mortgage loan"
"Loan Application"
根据您的要求,我会建议使用自定义函数,例如,checkMatch(str)
接受字符串和 return 找到或未找到匹配项的布尔值。
var strToMatch = "loan nation";
var str1 = "Loan Origination";
var str2 ="international loans";
var str3 = "home mortgage loan";
var str4 = "Loan Application";
function checkMatch(str){
var strToMatchArray = strToMatch.split(' ');
var matched = true;
for(var i=0; i<strToMatchArray.length; i++){
if(str.toLowerCase().indexOf(strToMatchArray[i].toLowerCase()) === -1){
matched = false;
break;
}
}
return matched;
}
//match
console.log(checkMatch(str1));
console.log(checkMatch(str2));
//do not match
console.log(checkMatch(str3));
console.log(checkMatch(str4));
可以使用正则表达式来比较和匹配一组单词吗?
比如我想要这样的字符串...
"loan nation"
匹配这些...
"Loan Origination"
"international loans"
but not on these...
"home mortgage loan"
"Loan Application"
根据您的要求,我会建议使用自定义函数,例如,checkMatch(str)
接受字符串和 return 找到或未找到匹配项的布尔值。
var strToMatch = "loan nation";
var str1 = "Loan Origination";
var str2 ="international loans";
var str3 = "home mortgage loan";
var str4 = "Loan Application";
function checkMatch(str){
var strToMatchArray = strToMatch.split(' ');
var matched = true;
for(var i=0; i<strToMatchArray.length; i++){
if(str.toLowerCase().indexOf(strToMatchArray[i].toLowerCase()) === -1){
matched = false;
break;
}
}
return matched;
}
//match
console.log(checkMatch(str1));
console.log(checkMatch(str2));
//do not match
console.log(checkMatch(str3));
console.log(checkMatch(str4));