Can a regular expressions be used to compare and match set of words?

49 Views Asked by At

Can a regular expressions be used to compare and match set of words?

For example, I want a string like this...
"loan nation"

to match on these...

"Loan Origination"

"international loans"

but not on these...

"home mortgage loan"

"Loan Application"
1

There are 1 best solutions below

2
On BEST ANSWER

Based on your requirement I will suggest to use a custom function, say, checkMatch(str) that takes the string and return the boolean value for the match found or not found.

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));