Dynamically match multiple words in a string to another one using regExp
<p>We all know that regExp can match two strings using a common word in both strings as criterion for the match. This code will match <em>"I like <strong>apples</strong>"</em> to <em>"<strong>apples</strong>"</em> because they both have "apples" in common.</p>

<pre><code>var str1 = "apples";
var test1 = "I like apples";
var reg1 = new RegExp(str1, "i");

if (test1.match(reg1)) {
    console.log(str1 + " is matched");
}
</code></pre>

<p>But what if instead of matching the strings using a single common word (or a common part of the strings), I need to match the two strings using multiple common words which may be separated by other words ? Let me show you an example :</p>

<p><code>test2</code> = <em>"I like <strong>juicy</strong> red <strong>fruits</strong>"</em> should match <code>str2</code> = <em>"<strong>juicy fruits</strong>"</em> because <code>test2</code> contains all of <code>str2</code> second key words (see object bellow), even though it has other words inbetween.</p>

<p>Tricky part is that I can't know exaclty what one the strings will be, so I have to match them dynamically. The string I don't know is the value of a user input field, and there are many possibilities. This value is to be matched to the strings stored in this object :</p>

<pre><code>var str2 = {
    "red apples": "fruits",
    "juicy fruits": "price : $10"
};
</code></pre>

<p>So whatever the user types in the input field, it must be a match if and only if it contains all the words of one of the object properties. So in this example, "<strong><em>juicy</strong> red and ripe <strong>fruits</em></strong>" should match the second object property, because it contains all of its keywords.</p>

<p>Edit : My goal is to output the value associated to the strings I'm matching. In my example if the first key is matched, 'fruits' should be output. If 'juicy fruits' is matched, it should output 'price : $10'. Getting the strings associated to the object keys is the reason why the user has to search for them using the input.</p>

<p>Is it possible to do this with regExp using pure javascript ?</p>

<p>Here is what I (poorly) tried to do : <a href="https://jsfiddle.net/Hal_9100/fzhr0t9q/1/" rel="nofollow">https://jsfiddle.net/Hal_9100/fzhr0t9q/1/</a></p>