Javascript regexp multiple matches seperated by a character <p>I'm using the awesomplete autosuggest plugin to show suggestions to an input. I want to match only from beginning of words, and match several places seperated by a space.</p> <p>Example:<br> input = "I don't know how to use RegExp correctly".</p> <p>If I type "kno reg", or "corr ho" - there should be a match.<br> but not if I type "kno ow", or "se corr".</p> <p>So far I have this to match only beginning of words:</p> <pre><code>_.FILTER_WORDSTART = function(text, input) { return RegExp("(?:^|\\s)" + $.regExpEscape(input.trim()), "i").test(text); }; $.regExpEscape = function (s) { return s.replace(/[-\\^$*+?.()|[\]{}]/g, "\\&amp;"); }; </code></pre> <p>This works, but I also need it to match several places separated by a space.</p> <p>(EDIT) Above part is solved.<br /> <br /> Now I have:</p> <pre><code>_.FILTER_WORDSTART = function(text, input) { return input.split(/\s+/).every(function(word) { return RegExp("(?:^|\\s)" + $.regExpEscape(word.trim()), "i").exec(text); }); }; $.regExpEscape = function (s) { return s.replace(/[-\\^$*+?.()|[\]{}]/g, "\\&amp;"); }; // Highlight function _.ITEM = function (text, input) { var html = input === '' ? text : text.replace(RegExp($.regExpEscape(input.trim()), "gi"), "&lt;mark&gt;$&amp;&lt;/mark&gt;"); return $.create("li", { innerHTML: html, "aria-selected": "false" }); }; </code></pre> <p>How do I get the highlight function to also highlight every word seperated by a space? The _.FILTER_WORDSTART will always return true or false, even if i set the "exec" on the regex instead of "test". But I need the actual match in the highlight function. Any help on how I would put this?</p>