0

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"
Gufran Hasan
  • 8,910
  • 7
  • 38
  • 51
Josh Ulm
  • 3
  • 1

1 Answers1

1

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));
Ankit Agarwal
  • 30,378
  • 5
  • 37
  • 62
  • as trying to solve for. I don't know why I thought I had to go through such this was exactly what I was looking for. well, not exactly because i'm writing Coffeescript. But it served the purpose I whoops, resorting to regular expressions, just to do this. But splitting the string into an array and then iterating through the other string makes perfect sense. Saved me a ton of time. Thanks. – Josh Ulm May 19 '18 at 20:52
  • for the record, I did convert this to Coffeescript. Looked something like this.
    `checkMatch = (str) -> strToMatchArray = strToMatch.split " " # break the search string up into an array matched = true for item, i in strToMatchArray if !(str.toLowerCase().match item.toLowerCase()) matched = false break return matched`
    – Josh Ulm May 19 '18 at 20:53