How to check if a string contains multiple words in different places

advertisements

So I have multiple string that are like this:

String 1: There is this one thing that I'm trying to do but I have no idea how.
String 2: I really have no clue how too fix it.
String 2: Hopefully maybe someone can help me.

Now I also have a string that is a search input that can be anything, for example:

There idea

When the user inputs and sends that I want the JavaScript too match with string 1.
Also I would like too have it return by how many characters the two strings are matching.

If someone can help me with this I would be very grateful.

Thanks in advance,

Job


If you want to know the length of the match, e.g. using regex:

var str = "There idea";

var pattern = new RegExp("\\b" + str.replace(/ +/g, "\\b.*\\b") + "\\b", "i");
console.log(pattern);

var input = new Array(
  "String 1: There is this one thing that I'm trying to do but I have no idea how.",
  "String 2: I really have no clue how too fix it. ",
  "String 3: Hopefully maybe someone can help me. "
);

for(i=0; i<input.length; i++) {
  if(res=input[i].match(pattern)) {
    console.log(res[0], res[0].length);
  }
}

\b matches a word boundary (zero-length match)